Calculator program in C using Switch Case

In this Calculator program in C, we are going to perform arithmetic operations (i.e., addition, subtraction, multiplication, and division) between two numbers in c language using switch case and do-while loop

Calculator program in C using switch case

#include <stdio.h>
int main()
{
	int a, b, ch;
	char choice;
	printf("\n Enter two numbers:");
	scanf("%d%d", &a, &b);
	do
	{
		printf("\n Press 1 to add %d and %d", a, b);
		printf("\n Press 2 to subtract %d and %d", a, b);
		printf("\n Press 3 to multiply %d and %d", a, b);
		printf("\n Press 4 to divide %d and %d", a, b);
		printf("\n Enter Your Choice: ");
		scanf("%d", &ch);
		switch (ch)
		{
		case 1:
			printf("Sum: %d", a + b);
			break;
		case 2:
			printf("Subtract :%d", a - b);
			break;
		case 3:
			printf("Multiply :%d", a * b);
			break;
		case 4:
			if (b == 0)
				printf("\n Denominator cannot be zero");
			else
				printf("Divide :%d", a / b); // b should not be zero
			break;
		default:
			printf("Wrong choice!");
		}
		printf("\n Do you want to continue? (Press y/n)");
		scanf(" %c", &choice);
	} while (choice == 'y');
	return 0;
}
Calculator program in C using Switch Case
Output:

Enter two numbers:5 3

Press 1 to add 5 and 3
Press 2 to subtract 5 and 3
Press 3 to multiply 5 and 3
Press 4 to divide 5 and 3
Enter Your Choice: 1
Sum: 8
Do you want to continue? (Press y/n)n

Approach to make a simple calculator program in c using switch case and do-while loop:-

  1. Initialize three numbers, one number is for the choice of the user and we perform arithmetic operations on the other two numbers.
  2. Input two numbers on which we perform arithmetic operations.
  3. Print all the choices and input the choice of the user.
  4. Using the switch keyword, we write all four cases for addition, subtraction, multiplication, and division respectively.
  5. Do not forget to write a break at the end of the case statement.
  6. You can also write a default statement in case the user input the wrong choice
  7. for example: - default:printf("Wrong choice!");

Program for Calculator in c using while loop

In this program, we are going to make a simple calculator using a while loop and using an if-else statement instead of a switch-case statement.

Calculator Program in C using if-else

#include<stdio.h>
#include<stdlib.h>
int main()
{
int num1, num2, choice;
while(1)
    {
        printf("\n Menu Driven Program in c");
        printf("\n 1.Addition");
        printf("\n 2.Subtraction");
        printf("\n 3.Multiplication");    
        printf("\n 4.Division");
        printf("\n 5.exit");
        printf("\n Enter Choice:");
        scanf("%d",&choice);    
    if(choice>0 && choice<6)
    {
    if (choice==1)
        {
            printf("\n Enter 1st number: ");
            scanf("%d",&num1);
            printf("\n Enter 2nd number: ");
            scanf("%d",&num2);
            printf("\n %d + %d = %d",num1,num2,num1+num2);
        }
    if (choice==2)
        {
            printf("\n Enter 1st number: ");
            scanf("%d",&num1);
            printf("\n Enter 2nd number: ");
            scanf("%d",&num2);
            printf("\n %d - %d = %d",num1,num2,num1-num2);
        } 
    
    if (choice==3)
        {
            printf("\n Enter 1st number: ");
            scanf("%d",&num1);
            printf("\n Enter 2nd number: ");
            scanf("%d",&num2);
            printf("\n %d * %d = %d",num1,num2,num1*num2);
        } 
     
    
    if (choice==4)
        {
            printf("\n Enter 1st number: ");
            scanf("%d",&num1);
            printf("\n Enter 2nd number: ");
            scanf("%d",&num2);
            if(num2==2)
            printf("\n Denominator cannot be zero");
            else
            {
                printf("\n %d / %d = ",num1,num2);
                printf("%d",num1/num2);
            }
        }
    if(choice==5)
        exit(0);
    }            
    else printf("\n Wrong choice");
    }
    return 0;
}

Output for Menu-driven Calculator Program using while-loop and if-else

Output:

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. exit
Enter Choice:1
Enter 1st number: 5
Enter 2nd number: 6
5 + 6 = 11

Simple C program to make a Calculator using a do-while loop

This code is given by Durgesh Kumar. He was facing a problem with the same program. He was not able to run his last line of code. See in the comment section of this post. Remember one thing, whenever you are taking a character input then you must write scanf(" %c", &choice);. You have to give a space between " & %.
#include <stdio.h>

int main()
{
    int num1, num2, opt;
    char choice;
    // asking the input from the users
    printf("enter the first intgers= ");
    scanf("%d", &num1);
    printf("enter the second intgers= ");
    scanf("%d", &num2);

    printf("*****************************");
    do
    {

        printf("\n please enter your choice. \n\n 1.Addtion\n 2.Subtrcation \n 3.Multiplication \n 4.division \n 5.Modulas\n");
        scanf("%d", &opt);
        switch (opt)
        {
        case 1:
            printf("The addition of %d + %d = %d", num1, num2, num1 + num2);
            break;
        case 2:
            printf("the substraction of %d - %d = %d", num1, num2, num1 - num2);
            break;
        case 3:
            printf("The multiplication of %d * %d = %d", num1, num2, num1 * num2);
            break;
        case 4:
            if (num2 == 0)
            {
                printf("this number can't divide by zero\n");
            }
            else
            {
                printf("the division of %d / %d = %d", num1, num2, num1 / num2);
            }
            break;
        case 5:
            if (num2 == 0)
            {
                printf("we can't find the remender");
            }
            else
            {
                printf("the remender of %d % %d = %d", num1, num2, num1 % num2);
            }
            break;
        default:
            printf("Please chosse the correct option");
        }

        printf("\ndo you want to continue? press(y/n)");
        scanf(" %c", &choice);
    } while (choice == 'y');
    return 0;
}

About this Calculator Program in C:

In conclusion, we have just gone through the different programs for calculators in c. It totally depends on your choice, but I will recommend you to make this program using a switch case. These types of programs are known as menu-driven programs.

Related Posts

2 comments

  1. good morning sir,

    this is Durgesh kumar. sir i have created a simple c program to make a calulator.
    but i have faced a problem and that is i am not able to run my last one code (do you continue? Press(y/n))
    here is my code
    // write a c program to make a sample calculater where operation will be choosed by users.

    #include
    #include

    int main()
    {
    int num1,num2,opt;
    char choice;
    // asking the input from the users
    printf("enter the first intgers= ");
    scanf("%d",&num1);
    printf("enter the second intgers= ");
    scanf("%d",&num2);

    printf("*****************************");
    do
    {

    printf("\n please enter your choice. \n\n 1.Addtion\n 2.Subtrcation \n 3.Multiplication \n 4.division \n 5.Modulas\n");
    scanf("%d",&opt);
    switch(opt)
    {
    case 1:
    printf("The addition of %d + %d = %d", num1,num2,num1+num2);
    break;
    case 2:
    printf("the substraction of %d - %d = %d",num1, num2,num1-num2);
    break;
    case 3:
    printf("The multiplication of %d * %d = %d", num1, num2, num1*num2);
    break;
    case 4:
    if(num2==0)
    {
    printf("this number can't divide by zero\n");
    }
    else
    {
    printf("the division of %d / %d = %d",num1,num2, num1/num2);
    }
    break;
    case 5:
    if(num2==0)
    {
    printf("we can't find the remender");
    }
    else
    {
    printf("the remender of %d % %d = %d",num1,num2, num1%num2);
    }
    break;
    default:
    printf("Please chosse the correct option");
    }

    printf("\ndo you want to continue? press(y/n)");
    scanf("%c",&choice);
    }
    while(choice=='y');
    return 0;




    }
    please tell me where i am doing mistake


    date- 03-04-2022 Thanks

    ReplyDelete
    Replies
    1. Replace scanf("%c", &choice); with scanf(" %c", &choice);

      Delete

Post a Comment