C Program: Make a Calculator by using switch Statement

Engg.... | 09:29 | | | Best Blogger Tips
calculator

Switch statement is very useful for creating a calculator which performs following operations: Addition, Substraction, Multiplication, Division and Modulus

#include<stdio.h>
#include<conio.h>
void main()
{
char a,b,c,d;
clrscr();
printf("Enter the values of a and b:");
scanf("%d%d" ,&a,&b);
printf(" Enter your choice between 1 to 5 /n");
scanf("%d , &d);
 
switch(d)                                        
{
case 1;
c=a+b;
printf(" Addition=%d", c);
break:
case 2;
c=a-b;
printf(" Substraction=%d" , c);
break:
case 3;
c=a*b;
printf(" Multiplication=%d", c);
break:
case 4;
c=a/b;
printf(" Divide=%d", c);
break:
case 5;
c=a%b;
printf(" Remainder=%d",c );
break:
default:
printf(" Invalid Result");
break:
}

Output1:
Enter the values of a and b:
3
5
Enter your choice between 1 to 5
1
Addition=8


Output2:
Enter the values of a and b:
5
5
Enter your choice between 1 to 5
3
Multiplication=25

Output3:
Enter the values of a and b:
50
10
Enter your choice between 1 to 5
4
Divide=5

Output4:
Enter the values of a and b:
8
2
Enter your choice between 1 to 5
5
Remainder=0

Output5:
Enter the values of a and b:
3
5
Enter your choice between 1 to 5
9
Invalid Result


Please Share it! :)
Do you like this Story?

Get Free Email Updates Daily!

Follow us!

23 comments:

  1. Variable d is a character not integer. So ...int d ..... not char d? Is this correct?

    ReplyDelete
  2. Its not "SUBsTRACTION" its "SUBTRACTION".

    ReplyDelete
  3. Program rightwork nhi ker rha he Program me start se thoda change hoga isme mistak he (#include #includ main() { int a,b,c,d; char ch;) ise chang kerne se program right work kreg

    ReplyDelete
  4. Wow, this "example" is so terrible I don't even know where to start

    Right off the bat this program won't compile for a number of reasons:
    - You're missing a quotation mark on the last scanf call: scanf("%d , &d);
    - Case statements are appended with COLON not semi-colon: 'case 1:' not 'case 1;'
    - Break statements are appended with a semi-colon, NOT a colon
    - There is no closing bracket for the main statement

    Even fixing these problems, the program still doesn't actually work.

    - You have '/n' instead of '\n' to denote a newline
    - As others have pointed out you're using char variables but using '%d' decimal integer format specifiers. It works, but converts values to ASCII and produces the complete wrong results. Most "calculations" of this poor excuse for a calculator just print out 0.
    - Absolutely no use of indentation or code formatting

    If the output you posted is indeed output, this was definitely not the source code that was used in compilation. Please at least make sure your source code compiles before posting it to the web for the world to see.

    ReplyDelete
  5. This information is very interesting, I really enjoyed, I would like get more information about this, because is very beautiful, thanks for sharing. novated lease calculator

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. #include
    #include
    void main()
    {
    int a,b,c;
    char op;
    clrscr();
    printf("Enter 1st Number :");
    scanf("%d",&a);

    printf("Enter Operator:\n");
    scanf("%c",&op);
    printf("Enter 2nd Number:");
    scanf("%d",&b);
    switch (op)
    {
    case '+':
    c=a+b;
    printf("Addition=%d",c);
    break;
    case '-':
    c=a-b;
    printf("Subtraction=%d",c);
    break;
    case '*':
    c=a*b;
    printf("Multiplication=%d",c);
    break;
    case '/':
    c=a/b;
    printf("Devision=%d",c);
    break;
    case '%':
    c=a%b;
    printf("Reminder=%d",c);
    break;
    default:
    printf("Wrong Input");
    }
    getch();
    }

    ReplyDelete
    Replies
    1. While executing this code after getting first number and the operator it directly skips to the default statement..
      Please help me out from this..
      Thanks in advance..

      Delete
    2. While executing this code after getting first number and the operator it directly skips to the default statement..
      Please help me out from this..
      Thanks in advance..

      Delete
  8. Keep it simple friend's
    %c is use for switch case
    Program work absolutely
    Regard:
    Ahsan Siddique


    #include
    #include
    void main()
    {
    int a,b,r;
    char op;

    printf("simply enter your input and operater);
    scanf("%d %c %d", &a, &op, &b);

    switch (op)
    {
    case '+':
    r=a+b;
    printf("\n Addition=%d\n",r);
    break;
    case '-':
    r=a-b;
    printf("\n Subtraction=%d\n",r);
    break;
    case '*':
    r=a*b;
    printf("\n Multiplication=%d\n",r);
    break;
    case '/':
    r=a/b;
    printf("\n Devision=%d\n",r);
    break;
    case '%':
    r=a%b;
    printf("\n Reminder=%d\n",r);
    break;
    default:
    printf("Wrong Input");
    }
    return (0);
    }

    ReplyDelete
  9. Superb work thanks for sharing.
    Regards. http://www.dishtvbazar.com/dish-tv-royle-hd.html

    ReplyDelete
  10. Calculator program in C

    In C language we can design a program to add, subtract, multiply, divide any number, these all operation you can perform by using switch case.

    ReplyDelete
  11. Total waste of time your programs have lot of errors

    ReplyDelete
  12. Total waste of time your programs have lot of errors

    ReplyDelete
  13. Programming is very interesting and creative thing if you do it with love. Your blog code helps a lot to beginners to learn programming from basic to advance level. I really love this blog because I learn a lot from here and this process is still continuing.
    Love from Pro Programmer

    ReplyDelete
  14. Programming is combination of intelligent and creative work. Programmers can do anything with code. The entire Programming tutorials that you mention here on this blog are awesome. Beginners Heap also provides latest tutorials of Programming from beginning to advance level.
    Be with us to learn programming in new and creative way.

    ReplyDelete