C – Code for Calculator Application

PrevNext

C code for a real time calculator application program is given below. This program will perform the below calculator operations.

2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Power
7. Factorial

……
In the below c code example, the user is prompted to choose the operations( i.e addition, subtraction etc)  to be performed and then prompted to key in the values which are used to perform the operations. Then, the result will be shown as output to the user.

C code for Calculator Application:

```// Calculator example using C code
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>

#define KEY "Enter the calculator Operation you want to do:"

// Function prototype declaration
void subtraction();
void multiplication();
void division();
void modulus();
void power();
int factorial();
void calculator_operations();

// Start of Main Program
int main()
{
int X=1;
char Calc_oprn;

// Function call
calculator_operations();

while(X)
{
printf("n");
printf("%s : ", KEY);

Calc_oprn=getche();

switch(Calc_oprn)
{
break;

case '-': subtraction();
break;

case '*': multiplication();
break;

case '/': division();
break;

case '?': modulus();
break;

case '!': factorial();
break;

case '^': power();
break;

case 'H':
case 'h': calculator_operations();
break;

case 'Q':
case 'q': exit(0);
break;
case 'c':
case 'C': system("cls");
calculator_operations();
break;

default : system("cls");

printf("n**********You have entered unavailable option");
printf("***********n");
printf("n*****Please Enter any one of below available ");
printf("options****n");
calculator_operations();
}
}
}

//Function Definitions

void calculator_operations()
{
//system("cls");  use system function to clear
printf("n             Welcome to C calculator nn");

printf("******* Press 'Q' or 'q' to quit ");
printf("the program ********n");
printf("***** Press 'H' or 'h' to display ");
printf("below options *****nn");
printf("Enter 'C' or 'c' to clear the screen and");
printf(" display available option nn");

printf("Enter + symbol for Addition n");
printf("Enter - symbol for Subtraction n");
printf("Enter * symbol for Multiplication n");
printf("Enter / symbol for Division n");
printf("Enter ? symbol for Modulusn");
printf("Enter ^ symbol for Power n");
printf("Enter ! symbol for Factorial nn");
}

{
int n, total=0, k=0, number;
printf("nEnter the number of elements you want to add:");
scanf("%d",&n);
printf("Please enter %d numbers one by one: n",n);
while(k<n)
{
scanf("%d",&number);
total=total+number;
k=k+1;
}
printf("Sum of %d numbers = %d n",n,total);
}

void subtraction()
{
int a, b, c = 0;
printf("nPlease enter first number  : ");
scanf("%d", &a);
printf("Please enter second number : ");
scanf("%d", &b);
c = a - b;
printf("n%d - %d = %dn", a, b, c);
}

void multiplication()
{
int a, b, mul=0;
printf("nPlease enter first numb   : ");
scanf("%d", &a);
scanf("%d", &b);
mul=a*b;
printf("nMultiplication of entered numbers = %dn",mul);
}

void division()
{
int a, b, d=0;
printf("nPlease enter first number  : ");
scanf("%d", &a);
printf("Please enter second number : ");
scanf("%d", &b);
d=a/b;
printf("nDivision of entered numbers=%dn",d);
}

void modulus()
{
int a, b, d=0;
printf("nPlease enter first number   : ");
scanf("%d", &a);
printf("Please enter second number  : ");
scanf("%d", &b);
d=a%b;
printf("nModulus of entered numbers = %dn",d);
}

void power()
{
double a,num, p;
printf("nEnter two numbers to find the power n");
printf("number: ");
scanf("%lf",&a);

printf("power : ");
scanf("%lf",&num);

p=pow(a,num);

printf("n%lf to the power %lf = %lf n",a,num,p);
}

int factorial()
{
int i,fact=1,num;

printf("nEnter a number to find factorial : ");
scanf("%d",&num);

if (num<0)
{
printf("nPlease enter a positive number to");
printf(" find factorial and try again. n");
printf("nFactorial can't be found for negative");
printf(" values. It can be only positive or 0  n");
return 1;
}

for(i=1;i<=num;i++)
fact=fact*i;
printf("n");
printf("Factorial of entered number %d is:%dn",num,fact);
return 0;
}```

.