Aim:
To write a simple program to develop a simple calculator that accepts two floating point numbers from the keyboard. Display a menu to the user and get the user’s choice. Perform the operation and display the result using switch statement.
Algorithm:
- Read the two floating point numbers A, B.
- Display the menu.
1 - Add
2 - Subtract
3 - Multiply
4 - Divide
- Get the user’s choice.
- If choice =1 then Result=A+B. Goto step 8.
- If choice =2 then Result=A-B. Goto step 8.
- If choice =3 then Result=A*B. Goto step 8.
- If choice =4 then Result=A/B. Goto step 8.
- Display the result.
- Stop.
Program:
/* To design simple calculator */
#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,res;
int ch;
clrscr();
printf("Enter the value of A:");
scanf("%f",&a);
printf("Enter the value of B(B<>0):");
scanf("%f",&b);
printf("\n\nMathematical Operations");
printf("\n************************");
printf("\n\t1->Add");
printf("\n\t2->Subtract");
printf("\n\t3->Multiply");
printf("\n\t4->Divide");
printf("\n\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:res=a+b;
break;
case 2:res=a-b;
break;
case 3:res=a*b;
break;
case 4:res=a/b;
break;
}
printf("Answer =%.2f",res);
getch();
}
Output 1:
Enter the value of A:5
Enter the value of B(B<>0):7
Mathematical Operations
************************
1->Add
2->Subtract
3->Multiply
4->Divide
Enter your choice:1
Answer =12.00
Output 2:
Enter the value of A:7
Enter the value of B(B<>0):56
Mathematical Operations
************************
1->Add
2->Subtract
3->Multiply
4->Divide
Enter your choice:2
Answer =-49.00
Output 3:
Enter the value of A:5.2
Enter the value of B(B<>0):3
Mathematical Operations
************************
1->Add
2->Subtract
3->Multiply
4->Divide
Enter your choice:3
Answer =15.60
No comments:
Post a Comment