# Menu Driven Calculator Program in C Using Functions

In the control flow, we created a simple calculator program using a switch case statement. Now, we will create a calculator program in C using functions.

Functions are used to divide a big problem into small subroutines. In this program, the user has the choice for operation, and it will continue until the user doesn’t want to exit from the program.

Prerequisites for this program:- Introduction to Function in C, User-defined Functions in C, C Program Using Functions Example

In this program, the calculator will perform add, subtract, multiply, divide, remainder, and power. To perform every operation one function will be created. Another function display() is used to print the result.

## Calculator program in C using functions

#include<stdio.h>
#include<stdlib.h>

// function declarations
void display(float n1, float n2, char ch, float result);
void subtract(float n1, float n2);
void multiply(float n1, float n2);
void divide(float n1, float n2);
void rem(float n1, float n2);
void power(float n1, float n2);

// main function
int main()
{
float n1, n2;
int ch;

do{
printf("Enter two numbers: ");
scanf("%f %f", &n1, &n2);

printf("\n*****************");
printf("\n2.Subtraction");
printf("\n3.Multiplication");
printf("\n4.Division");
printf("\n5.Remainder");
printf("\n6.Power (x^y)");
printf("\n7.Exit");
scanf("%d", &ch);

switch (ch) {
case 1:
break;
case 2:
subtract(n1,n2);
break;
case 3:
multiply(n1,n2);
break;
case 4:
divide(n1,n2);
break;
case 5:
rem(n1,n2);
break;
case 6:
power(n1,n2);
break;
case 7:
printf("Thank You.");
exit(0);
default:
printf("Invalid input.");
}

printf("\n**********************************\n");
}while(1);

return 0;
}

// function for displaying the result
void display(float n1, float n2, char ch, float result)
{
printf("%.2f %c %.2f = %.2f\n", n1, ch, n2, result);
}

// function for addition of two numbers
{
float result = n1 + n2;
display(n1, n2, '+', result);
}

// function for subtraction of two numbers
void subtract(float n1, float n2)
{
float result = n1 - n2;
display(n1, n2, '-', result);
}

// function for multiplication of two numbers
void multiply(float n1, float n2)
{
float result = n1 * n2;
display(n1, n2, '*', result);
}

// function for division of two numbers
void divide(float n1, float n2)
{
float result = n1 / n2;
display(n1, n2, '/', result);
}

// function for calculating remainder
void rem(float n1, float n2)
{
//Modulus operator only works on int data type
//Floating numbers are converted to int number
int num1 = n1;
int num2 = n2;
int result = num1%num2;
printf("%d %% %d = %d\n", num1, num2, result);
}

// function for calculating power
void power(float n1, float n2)
{
if(n2<0) printf("Second number should be +ve.");
else
{
float result=1.0;
for(int i=1; i<=n2; i++)
{
result *= n1;
}
display(n1, n2, '^', result);
}
}

Sample of Output:-

Enter two numbers: 20 10

*****************
2.Subtraction
3.Multiplication
4.Division
5.Remainder
6.Power (x^y)
7.Exit
20.00 + 10.00 = 30.00

*****************************************
Enter two numbers: 25 15

*****************
2.Subtraction
3.Multiplication
4.Division
5.Remainder
6.Power (x^y)
7.Exit
25.00 – 15.00 = 10.00

*****************************************
Enter two numbers: 15 4

*****************
2.Subtraction
3.Multiplication
4.Division
5.Remainder
6.Power (x^y)
7.Exit
15 % 4 = 3
*****************************************
Enter two numbers: 5 -3

*****************
2.Subtraction
3.Multiplication
4.Division
5.Remainder
6.Power (x^y)
7.Exit