Menu Driven Calculator

Menu Driven Calculator

Program:


#include<stdio.h>
void main()
{
int a,b,n;
float sum,mult,sub,div,mod;
printf("Enter 2 numbers: ");
scanf("%d%d",&a,&b);
printf("Enter your Choice: \n 1. Add\n 2. Substract \n 3. Multiply\n 4. Divide\n 5. Remainder\n ");
scanf("%d",&n);
switch(n)
{
case 1:
sum=(float)a+b;
printf("Addition is: %f\n",sum);
break;
case 2:
sub=(float)a-b;
printf("Substraction is: %f\n",sub);
break;
case 3:
mult=(float)a*b;
printf("Multiplication is: %f\n",mult);
break;
case 4:
div=(float)a/b;
printf("Division is: %f\n",div);
break;
case 5:
mod=a%b;
printf("Remainder  is: %f\n",mod);
break;
default:
printf("Invalid!!!");
}
}

Explanation:
In some cases we have to give user choice of options to choose.
Similarly, here we have asked user what he wants to do with entered numbers and thereby, we can do further operations and give user desired output.





Note: 
We have used type conversion for all operations except for modulo operator. If we use float numbers directly then we can't perform modulo operation.

Output:




No comments:

Post a Comment