Menu-driven Calculator
C Program
1.Using if else statement
#include <stdio.h>
#include <stdlib.h>
int main()
{
float num1,num2;
int choice;
printf("Enter First Number:");
scanf("%f",&num1);
printf("Enter Second Number:");
scanf("%f",&num2);
printf("press:\n");
printf("1.For Addition\n2.For Subtration\n3.For Division\n4.For Multiplication\n");
scanf("%d",&choice);
if(choice==1)
printf("Addition of %f and %f is %f",num1,num2,(num1+num2));
else if(choice==2)
printf("Subtraction of %f from %f is %f",num1,num2,(num1-num2));
else if(choice==3)
printf("Division of %f from %f is %f",num1,num2,(num1/num2));
else if(choice==4)
printf("Multiplication of %f and %f is %f",num1,num2,(num1*num2));
else
printf("Please enter correct choice");
}
2.Using Switch statement
#include <stdio.h>
#include <stdlib.h>
int main()
{
float num1,num2;
int choice;
printf("Enter First Number:");
scanf("%f",&num1);
printf("Enter Second Number:");
scanf("%f",&num2);
printf("press:\n");
printf("1.For Addition\n2.For Subtration\n3.For Division\n4.For Multiplication\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Addition of %f and %f is %f",num1,num2,(num1+num2));
break;
case 2: printf("Subtraction of %f from %f is %f",num1,num2,(num1-num2));
break;
case 3: printf("Division of %f from %f is %f",num1,num2,(num1/num2));
break;
case 4: printf("Multiplication of %f and %f is %f",num1,num2,(num1*num2));
break;
default: printf("Please enter correct choice");
}
}
0 Comments:
Post a Comment