Calculator Program In C

Algorithm of Calculator Program

Declare four local variables in the program: n1, n2, res, and opt. Assign numeric values to n1 and n2, store the results in the variable res, and use the variable opt to represent the operator symbols in the calculations. This setup allows for efficient computation based on the provided inputs.

Print the selected operation choice, such as addition, subtraction, multiplication, division, etc.

Step 3: Enter the Choice

Step 4: Takes two numbers, n1 and n2

Step 5: Implement a switch-case statement to navigate to a specific operation chosen by the user.

Step 6: Store result into res variable.

Step 7: Display the operation result

Step 8: Exit from the program.

Different ways to create a Calculator Program in C

Following are the different ways to write a Calculator Program in the C language.

  • Calculator Program in C using the switch statement
  • Calculator Program in C using if else if statement
  • Calculator Program in C using do-while loop and switch statement
  • Calculator Program in C using function and switch statement
  • Example 1: Calculator Program in C using the switch statement

Let's develop a program to build a Calculator application utilizing a switch statement.

program.c

Example

#include <stdio.h>
int main()
{
	// declare local variables
	char opt;
	int n1, n2; 
	float res;
	printf (" Choose an operator(+, -, *, /) to perform the operation in C Calculator \n ");
	scanf ("%c", &opt); // take an operator
	if (opt == '/' )
	{
		printf (" You have selected: Division");
	}
	else if (opt == '*')
	{
		printf (" You have selected: Multiplication");
	 }
	 
	else if (opt == '-')
	{
		printf (" You have selected: Subtraction");
	 }
	 	else if (opt == '+')
	{
		printf (" You have selected: Addition");
	 }   
	printf (" \n Enter the first number: ");
	scanf(" %d", &n1); // take fist number
	printf (" Enter the second number: ");
	scanf (" %d", &n2); // take second number
	
	switch(opt)
	{
		case '+':
			res = n1 + n2; // add two numbers
			printf (" Addition of %d and %d is: %.2f", n1, n2, res);
			break;
		
		case '-':
			res = n1 - n2; // subtract two numbers
			printf (" Subtraction of %d and %d is: %.2f", n1, n2, res);
			break;
			
		case '*':
			res = n1 * n2; // multiply two numbers
			printf (" Multiplication of %d and %d is: %.2f", n1, n2, res);
			break;			
		
		case '/':
			if (n2 == 0)   // if n2 == 0, take another number
			{
				printf (" \n Divisor cannot be zero. Please enter another value ");
				scanf ("%d", &n2);		
				}
			res = n1 / n2; // divide two numbers
			printf (" Division of %d and %d is: %.2f", n1, n2, res);
			break;
		default:  /* use default to print default message if any condition is not satisfied */
			printf (" Something is wrong!! Please check the options ");				
	}
	return 0;
}

Output:

Output

Choose an operator(+, -, *, /) to perform the operation in C Calculator 
  You have selected: Division You have selected: Multiplication You have selected: Subtraction You have selected: Addition 
 Enter the first number:  Enter the second number:  Addition of [value] and [value] is: %.2f Subtraction of [value] and [value] is: %.2f Multiplication of [value] and [value] is: %.2f 
 Divisor cannot be zero. Please enter another value  Division of [value] and [value] is: %.2f Something is wrong!! Please check the options

Example 2: Calculator Program in C using if else if statement

Let's explore an illustration to craft a basic Calculator program in C employing if else if statements.

program2.c

Example

#include <stdio.h>
int main()
{
	// declare local variables
	char opt;
	int n1, n2; 
	float res;
	printf (" Select an operator (+, -, *, /) to perform an operation in C calculator \n ");
	scanf ("%c", &opt); // take an operator
	printf (" Enter the first number: ");
	scanf(" %d", &n1); // take fist number
	printf (" Enter the second number: ");
	scanf (" %d", &n2); // take second number
	
	if (opt == '+')
	{
		res = n1 + n2; // add two numbers
		printf (" Addition of %d and %d is: %f", n1, n2, res);
	}
	
	else if (opt == '-')
	{
		res = n1 - n2; // subtract two numbers
		printf (" Subtraction of %d and %d is: %f", n1, n2, res);
	}
	
	else if (opt == '*')
	{
		res = n1 * n2; // multiply two numbers
		printf (" Multiplication of %d and %d is: %f", n1, n2, res);
	}
	
	else if (opt == '/')
	{
		if (n2 == 0)   // if n2 == 0, take another number
		{
			printf (" \n Divisor cannot be zero. Please enter another value ");
			scanf ("%d", &n2);		
		}
		res = n1 / n2; // divide two numbers
		printf (" Division of %d and %d is: %.2f", n1, n2, res);
	}
	else
	{
		printf(" \n You have entered wrong inputs ");
	}
	return 0;
}

Output:

Output

Choose an operator(+, -, *, /) to perform the operation in C Calculator 
  You have selected: Division You have selected: Multiplication You have selected: Subtraction You have selected: Addition 
 Enter the first number:  Enter the second number:  Addition of [value] and [value] is: %.2f Subtraction of [value] and [value] is: %.2f Multiplication of [value] and [value] is: %.2f 
 Divisor cannot be zero. Please enter another value  Division of [value] and [value] is: %.2f Something is wrong!! Please check the options

Example 3: Calculator Program in C using do while loop and switch statement

Let's develop a program for a Calculator using a do-while loop and a switch-case statement in the C programming language.

Here is how we can achieve this:

Example

#include <stdio.h>

int main() {
    char operator;
    float num1, num2;

    do {
        printf("Enter an operator (+, -, *, /) or 'q' to quit: ");
        scanf(" %c", &operator);

        if (operator == 'q') {
            break;
        }

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

        switch(operator) {
            case '+':
                printf("%.2f + %.2f = %.2f\n", num1, num2, num1 + num2);
                break;
            case '-':
                printf("%.2f - %.2f = %.2f\n", num1, num2, num1 - num2);
                break;
            case '*':
                printf("%.2f * %.2f = %.2f\n", num1, num2, num1 * num2);
                break;
            case '/':
                if(num2 != 0) {
                    printf("%.2f / %.2f = %.2f\n", num1, num2, num1 / num2);
                } else {
                    printf("Error! Division by zero.\n");
                }
                break;
            default:
                printf("Error! Invalid operator.\n");
        }
    } while(1);

    return 0;
}

program3.c

Example

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main()
{
	// declaration of local variable op;
	int op, n1, n2;
	float res;
	char ch;
	do
	{
		// displays the multiple operations of the C Calculator
		printf (" Select an operation to perform the calculation in C Calculator: ");
		printf (" \n 1 Addition  \t \t 2 Subtraction \n 3 Multiplication \t 4 Division \n 5 Square \t \t 6 Square Root \n 7 Exit \n \n Please, Make a choice ");	
		
		scanf ("%d", &op); // accepts a numeric input to choose the operation
	
	
	// use switch statement to call an operation
	switch (op)
	{
		case 1:
			// Add two numbers
			printf (" You chose: Addition");
			printf ("\n Enter First Number: ");
			scanf (" %d", &n1);
			printf (" Enter Second Number: ");
			scanf (" %d", &n2);
			res = n1 + n2; // Add two numbers
			printf (" Addition of two numbers is: %.2f", res);
			break; // break the function
			
		case 2:
			// Subtract two numbers
			printf (" You chose: Subtraction");
			printf ("\n Enter First Number: ");
			scanf (" %d", &n1);
			printf (" Enter Second Number: ");
			scanf (" %d", &n2);
			res = n1 - n2; // subtract two numbers
			printf (" Subtraction of two numbers is: %.2f", res);
			break; // break the function
			
		case 3:
			// Multiplication of the numbers
			printf (" You chose: Multiplication");
			printf ("\n Enter First Number: ");
			scanf (" %d", &n1);
			printf (" Enter Second Number: ");
			scanf (" %d", &n2);
			res = n1 * n2; // multiply two numbers
			printf (" Multiplication of two numbers is: %.2f", res);
			break; // break the function
			
		case 4:
			// Division of the numbers
			printf (" You chose: Division");
			printf ("\n Enter First Number: ");
			scanf (" %d", &n1);
			printf (" Enter Second Number: ");
			scanf (" %d", &n2);
			if (n2 == 0)
				{
					printf (" \n Divisor cannot be zero. Please enter another value ");
					scanf ("%d", &n2);		
				}
			res = n1 / n2; // divide two numbers
			printf (" Division of two numbers is: %.2f", res);
			break; // break the function
			
		case 5:
			// getting square of a number
			printf (" You chose: Square");
			printf ("\n Enter First Number: ");
			scanf (" %d", &n1);

			res = n1 * n1; // get square of a number
			printf (" Square of %d number is: %.2f", n1, res);
			break; // break the function
			
		case 6:
			// getting the square root of the number
			printf (" You chose: Square Root");
			printf ("\n Enter First Number: ");
			scanf (" %d", &n1);
			
			res = sqrt(n1); // use sqrt() function to find the Square Root 
			printf (" Square Root of %d numbers is: %.2f", n1, res);
			break; // break the function
			
		case 7:
			printf (" You chose: Exit");
			exit(0); 
			break; // break the function
			
		default:
			printf(" Something is wrong!! ");
			break; 						
	}
	printf (" \n \n ********************************************** \n ");
	} while (op != 7);

	return 0;      
}

Output:

Output

Choose an operator(+, -, *, /) to perform the operation in C Calculator 
  You have selected: Division You have selected: Multiplication You have selected: Subtraction You have selected: Addition 
 Enter the first number:  Enter the second number:  Addition of [value] and [value] is: %.2f Subtraction of [value] and [value] is: %.2f Multiplication of [value] and [value] is: %.2f 
 Divisor cannot be zero. Please enter another value  Division of [value] and [value] is: %.2f Something is wrong!! Please check the options

Example 4: Calculator Program in C using function and switch statement

Let's develop a Calculator program using functions and a switch case statement in the C programming language.

program4.c

Example

#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
	
// function declarations 
int addition();
int subtract();
int multiply();
int divide();
int sq();
int sqrt1();
void exit();

int main()
{
	// declaration a local variable op;
	int op;
	do
	{
		// displays the multiple operations of the C Calculator
		printf (" Select an operation to perform the calculation in C Calculator: ");
		printf (" \n 1 Addition  \t \t 2 Subtraction \n 3 Multiplication \t 4 Division \n 5 Square \t \t 6 Square Root \n 7 Exit \n \n Please, Make a choice ");	
		
		scanf ("%d", &op); // accepts a numeric input to choose the operation
	
	
	// use switch statement to call an operation
	switch (op)
	{
		case 1:
			addition(); /* It call the addition() function  to add the given numbers */
			break; // break the function
			
		case 2:
			subtract(); /* It call the subtract() function  to subtract the given numbers */
			break; // break the function
			
		case 3:
			multiply(); /* It call the multiply() function  to multiply the given numbers */
			break; // break the function
			
		case 4:
			divide(); // It call the divide() function  to divide the given numbers
			break; // break the function
			
		case 5:
			sq(); // It call the sq() function  to get the square of given numbers
			break; // break the function
			
		case 6:
			sqrt1(); /* It call the sqrt1() function  to get the square root of given numbers */
			break; // break the function
			
		case 7:
			exit(0); // It call the exit() function  to exit from the program
			break; // break the function
			
		default:
			printf(" Something is wrong!! ");
			break; 						
	}
	printf (" \n \n ********************************************** \n ");
	} while (op != 7);
	

	return 0;      
}



// function definition
int addition()
{
	int i, sum = 0, num, f_num; // declare a local variable	
	printf (" How many numbers you want to add: ");
	scanf ("%d", &num);
	printf (" Enter the numbers: \n ");
	for (i = 1; i <= num; i++)
	{
		scanf(" %d", &f_num);
		sum = sum + f_num;
	}
	printf (" Total Sum of the numbers = %d", sum);
	return 0;
}

// use subtract() function to subtract two numbers
int subtract()
{
	int n1, n2, res;
	printf (" The first number is: ");
	scanf ("  %d", &n1);
	printf (" The second number is: ");
	scanf ("  %d", &n2);
	res = n1 - n2;	
	printf (" The subtraction of %d - %d is: %d", n1, n2, res);
}

// use multiply() function to multiply two numbers
int multiply()
{
	int n1, n2, res;
	printf (" The first number is: ");
	scanf ("  %d", &n1);
	printf (" The second number is: ");
	scanf ("  %d", &n2);
	res = n1 * n2;	
	printf (" The multiply of %d * %d is: %d", n1, n2, res);
}

// use divide() function to divide two numbers
int divide()
{
	int n1, n2, res;
	printf (" The first number is: ");
	scanf ("  %d", &n1);
	printf (" The second number is: ");
	scanf ("  %d", &n2);
	
	if (n2 == 0)
	{
		printf (" \n Divisor cannot be zero. Please enter another value ");
		scanf ("%d", &n2);		
	}
	res = n1 / n2;	
	printf (" \n The division of %d / %d is: %d", n1, n2, res);
}

// use sq() function to get the square of the given number
int sq()
{
	int n1, res;
	printf (" Enter a number to get the Square: ");
	scanf ("  %d", &n1);
	
	res = n1 * n1;	
	printf (" \n The Square of %d is: %d", n1, res);
}

// use sqrt1() function to get the square root of the given number 
int sqrt1()
{
	float res;
	int n1;
	printf (" Enter a number to get the Square Root: ");
	scanf ("  %d", &n1);

	res = sqrt(n1);	
	printf (" \n The Square Root of %d is: %f", n1, res);
}

Output:

Input Required

This code uses input(). Please provide values below: