To Accept Two Numbers and Perform Various Arithmetic Operations using Switch with Algorithm

In this post, we are going to make a program in c to accept two numbers from the user and perform various arithmetic operations using the switch function.



Algorithm:-

1. We write Header File & Math File
2. Declare a,b, and choice as Integer.
3. Declare c,d as Float.
4. We use a whole loop with the condition.,
5. Write the menu.
6. Take input in choice.
7. Write all cases if the user type 1 then Add two numbers
8 Print the Output


Code

#include <stdio.h>
#include <math.h>
int main()
{
int a,b,choice;
float c,d;
while(choice!=5)
{
printf("\n Calculator");
printf("\n1.Addition");
printf("\n2.Subtraction");
printf("\n3.Multiplication");
printf("\n4.Division");
printf("\n5.Modulus Division");
printf("\n6.Break");
printf("\n----------------------------------------------------------------------------------------");
printf("\n Enter your choice");
scanf("%d",&choice);
switch(choice)
{
case(1):
printf("\n Enter two numbers");
scanf("%d%d",&a,&b);
printf("\n Addition of two numbers is %d",a+b);
break;
case(2):
printf("\n Enter two numbers");
scanf("%d%d",&a,&b);
printf("\n Subtraction of two numbers is %d",a-b);
break;
case(3):
printf("\n Enter two numbers");
scanf("%d%d",&a,&b);
printf("\n Multiplication of two numbers is %d",a*b);
break;
case(4):
printf("\n Enter two numbers");
scanf("%d%d",&a,&b);
printf("\n Division of two numbers is %d",a/b);
break;
case(5):
printf("\Enter two numbers");
scanf("%d%d",&a,&b); 
printf("\n Remainder of two numbers is %d",a%b);
break;
case(6):
break;
default:
printf("INCORRECT CHOICE!!!!!!!!!!!!!");
break;
}
}
return 0;
}

Output

Calculator
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Modulus Division
6.Break
----------------------------------------------------------------------------------------
 Enter your choice3
 Enter two numbers99
 10
 Multiplication of two numbers is 990
 Calculator
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Modulus Division
6.Break
----------------------------------------------------------------------------------------
 Enter your choice

Post a Comment

Previous Post Next Post