Simple Calculator Program in C

This is the simple calculator program in c. It performs addition, subtraction, multiplication, and division of integer values.

Simple Calculator Program in C

Source Code:

// simple calculator program in c
#include <stdio.h>

// calci function body 
int calci(int num1, int num2, int ch){
       int res;

       // Swithc case for the operations
       switch (ch){

             // Addition
             case 1:
             res = num1 + num2;
             break;

             // Subtration
             case 2:
             res = num1 - num2;
             break;

             // Multiplication
             case 3:
             res = num1 * num2;
             break;

             // Division 
             case 4:
             res = num1 / num2;
             break;

            // If choice is incorrect
            default:
            printf("\nEnter correct choise from(1-4)");
     }

     // Return result of operation to main
     return res;
}

void main() {
       // declare variables used in program 
       int num1, num2, res, ch;

       // Enter first number
       printf("Enter the first number=");
       scanf("%d",&num1);

       // Enter second number
       printf("Enter the second number=");
       scanf("%d",&num2);

       // Choose your operation to perform form (1-4)
       printf("\n\n1: Addition(+)\n2: Subtraction(-)\n3: Multiplication(X)\n4: Division(÷)\n\nEnter your choice:");
       scanf("%d",&ch);

       // Calling calci funtion for operation 
      // And store result of returned result by calci function 
      res = calci(num1,num2,ch);

      // statement to print the result of operation
      printf("\n\nThis is your result for chosen operation:%d",res);
}

Input:

Enter the first number=12
Enter the second number=12


1: Addition(+)
2: Subtraction(-)
3: Multiplication(X)
4: Division(÷)

Enter your choice:1

Output:

This is your result for chosen operation:24

Recommended Post