Program to implement a calculator by switch case in C-language

Program to implement a calculator by switch case in C-language


                             Method-1
#include<stdio.h>
int main()
{
float x,y;
char ch;
printf("Enter first number\n");
scanf("%f",&x);
printf("Enter second number\n");
scanf("%f",&y);
printf("Enter\n+ for add\n- for sub\n* for multiply\n/ for div\n");
scanf("\n%c",&ch);
switch(ch)
{
case '+':
printf("Add=%f",(x+y));
break;
case '-':
printf("Sub=%f",(x-y));
break;
case '*':
printf("Multiply=%f",(x*y));
break;
case '/':
printf("Div=%f",(x/y));
break;
default:
printf("Invalid input!!");
}

}

                                 Method-2
#include<stdio.h>
int main()
{
float x,y;
char ch;
printf("Enter first number\n");
scanf("%f",&x);
printf("Enter second number\n");
scanf("%f",&y);
printf("Enter\n1 for add\n2 for sub\n3 for multiply\n4 for div\n");
scanf("\n%c",&ch);
switch(ch)
{
case '1':
printf("Add=%f",(x+y));
break;
case '2':
printf("Sub=%f",(x-y));
break;
case '3':
printf("Multiply=%f",(x*y));
break;
case '4':
printf("Div=%f",(x/y));
break;
default:
printf("Invalid input!!");
}
}

OUTPUT
Enter first number
7
Enter second number
1
Enter
1 for add
2 for sub
3 for multiply
4 for div
1
Add=8.000000

This program give you a rough idea about calculator working.And this program is also famous problem of switch case.

If you like this program then you can share it and for any issues you can direct contact with me from Home Page.

For regular updates you can also follow my site.


Comments

  1. Learnt it very quickly and easily. Thank You Sir

    ReplyDelete
  2. Wow sir the program worked nicely

    ReplyDelete

Post a Comment

Popular posts

Creative pattern of butterfly in C-language

Program to show present time and date in C-language

Introduction of C-language.

Program to cheque enter number is a perfect number or not using if else in C-language

Program to cheque four digits number is palindrome or not in C-language

Program to print the cube of a number in c-language

Simple Hello world program in c- language