How I Make Calculator In C Programming Language || Calculator in C Using Switch Case Full Explained

 Calculator in C

How I Make Calculator In C Programming Language || Calculator in C Using Switch Case Full Explained in Hindi. This program will read two integer numbers and an operator like +,-,*,/,% and then print the result according to given operator, it is a complete calculator program on basic arithmetic operators using switch statement in c programming language. This video is made for beginners in C Programming language to help them make simple Calculator Tutorial using switch case, if else, loops, functions and many more. This give full C Programming Tutorial to make calculator using Switch case explained in Hindi.

source code :

 //This is program to code a simple calculator

#include<stdio.h>
#include<math.h>
void calc(int a,char op,int b);
int main(){
    int a,b,repeat;
    char op;
    printf("enter the 1st operand : ");
    scanf("%d",&a);
    printf("enter the operator : ");
    getchar();           //enter is also a char so to give in this func
    scanf("%c",&op);
    printf("enter the 2nd operand : ");
    scanf("%d",&b);
    calc(a,op,b);
    do{
        printf("\nfor running again enter 1 and for end enter 2 : ");
        scanf("%d",&repeat);
        if(repeat==1){
            printf("enter the 1st operand : ");
    scanf("%d",&a);
    printf("enter the operator : ");
    getchar();           //enter is also a char so to give in this func
    scanf("%c",&op);
    printf("enter the 2nd operand : ");
    scanf("%d",&b);
    calc(a,op,b);
        }
    }while(repeat ==1 || repeat!=2);

    return 0;
}

void calc(int a,char op,int b){
    int result;
    switch (op)
    {
    case '+':
        result = a+b;
        printf("%d + %d = %d",a,b,result);
        break;
    case '-':
        result = a-b;
        printf("%d - %d = %d",a,b,result);
        break;
    case '*':
        result = a*b;
        printf("%d * %d = %d",a,b,result);
        break;
    case '/':
    if(b==0){
        printf("can't divide. ");
    break;
    }
    else{
        result = a/b;
        printf("%d / %d = %d",a,b,result);
        break;
    }
    case '%':
        result = a%b;
        printf("remainder of %d / %d = %d",a,b,result);
        break;
    case '^':
    result = pow(a,b);
    printf("%d ^ %d = %d",a,b,result);
    break;
    
    default:
    printf("please enter valid operator ");
        break;
    }

}

Previous
Next Post »