Make Simple Calculator in C Programming Language

 Hey there! We're very glad to see you here. First of all you're the most welcome on our platform Codexun. In the today's topic we are going to make simple calculator program using c language. In this we will use the switch case or if else case of our c programming language and will make simple calculator which will help us to do the arithmetic calculations like addition, subtraction, multiplication and  division. So without of wasting too much time let's get start it.



Make calculator program using c language

To make this program we can use different type of statements in our c language. For example we can use if else statement or switch statement. Here we used the switch case statement to make our calculator. Below is the whole code of calculator program, You can just copy it from below and can use it anywhere. Below that code we gives explaination of whole program.

Calculator Using C
    
    #include <stdio.h>
    int main()
   {
     char ch;
     int num1,num2;

     printf("Choose the operator(+,-,*,/,%%): ");
     scanf("%c",&ch);

     printf("Enter two numbers: ");
     scanf("%d %d",&num1,&num2);

     switch(ch)
     {
       case '+':
         printf("%d + %d =\t%d\n",num1,num2,num1+num2); 
         break;
       case '-':
         printf("%d - %d =\t%d\n",num1,num2,num1-num2);
         break;
       case '*':
         printf("%d * %d =\t%d\n",num1,num2,num1*num2);
         break;
       case '/':
         printf("%d / %d =\t%d\n",num1,num2,num1/num2);
         break;
       case '%':
         printf("%d %% %d =\t%d\n",num1,num2,num1%num2);
         break;
       default:
         printf("Error! Invalid Operator.");
     }

     return 0;
 }


Explanation

The switch case statement is used to write a above simple calculator program in c language. In the above program first we declare the three variables first is character ch which is used to store the character value and remaining two variables are num1 and num2 which will be get used to store 2 different numbers.

After declaring the variables in the printf statement we ask user to enter their operator of equation they wanna to do for example for addition enter  +  symbol. This user entered operator will get saved in ch variable through scanf statement. After that we ask user to enter their two numbers and now they will get saved in num1 and num2 variable. And finally with the help of this values our program means compiler will print the final output to the user on their screen. If any operator value is not matched to our operator then switch statement default statement will executed which is "Error Invalid Operator" to the users screen.

Conclusion

At the end of article we just hope you understand the how to make simple calculator program using c language. If you have any type of question or queries related to above article then feel free to comment below, we will try our best to solve your query. So now what are you waiting for? Share this article with your friends and family who really have the need of this and help them to solve their questions. Thank you!

Regards, Team Codexun.