Write a program to make calculator using 'Switch' in C Language .

 Today we are Learn how to make calulator using switch case in “c language” here am going to tell you about uses of switch case and sintex of swetch case then after we make a calculater using that.

“Switch” case is little bet same as “if” and “else”  in switch we are use “case”  and case is run when condition is true and condition is false then “default” case is run . you understand more better when we are write sintex and code using those keywords.

First we write switch keyword sintex and then write programme to make calculater using switch keyword…

                   



1.   Sintex for switch keyword

switch (expression)
{
case /* constant-expression */:
    /* code */
    break;

default:
    break;
}

Now I hope so you are understand switch keyword and now am going to tell you uses of “break”

Basically “Breck” use in loops and switch to stop the “opration” yes this is very simple thing and very simple uses too.

Now we are Write a program using switch keyword and make a  beautiful calculater

So let’s started….

#include<stdio.h>
int main(){
    printf("enter a value to calulate\n\n");
    int a,b;
    printf("enter the value of a=");
    scanf("%d",&a);

    printf("enter the value of b=");
    scanf("%d",&b);
     printf("\n");

    char h;
    printf("for addition enter '+'\n");
    printf("for substraction enter '-'\n");
    printf("for multiplication enter '*'\n");
    printf("for division enter '/'\n\n");
     
     scanf("%c",&h);
     scanf("%c",&h);
     

     switch(h){
         case '+':
         printf("%d+%d=%d",a,b,a+b);
         break;

         case '-':
         printf("%d-%d=%d",a,b,a-b);
         break;

         case '*':
         printf("%d*%d=%d",a,b,a*b);
         break;

         case '/':
         printf("%d/%d=%d",a,b,a/b);
         break;

         default:
         printf("enter valid symbol");

     }
     return 0;
}


Output:-

enter a value to calulate

enter the value of a=23

enter the value of b=7

 

for addition enter '+'

for substraction enter '-'

for multiplication enter '*'

for division enter '/'

 

*

23*7=161

 


 


0 Comments