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

Syntax of C-language

Variable & Datatype for C-programming

Language Translator of C-language

Errors in C-language

Addition of two numbers in c language

Introduction of C-language.

Linear search in C-language

Binary searching in C-language

Program to reverse one array into other array in C-language