Sunday, 29 November 2015

To design simple Calculator in C

Share it Please

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:
  1. Read the two floating point numbers A, B.
  2. Display the menu.
1 - Add
    2 - Subtract
3 - Multiply
4 - Divide
  1. Get the user’s choice.
  2. If choice =1 then Result=A+B. Goto step 8.
  3. If choice =2 then Result=A-B. Goto step 8.
  4. If choice =3 then Result=A*B. Goto step 8.
  5. If choice =4 then Result=A/B. Goto step 8.
  6. Display the result.
  7. 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

Blogroll

About

Designed By IAMNGP | Moon