Arithmetic Operation Using Switch Statement in C

Introduction:

The switch statement allows a program to perform different arithmetic operations based on the user's choice. It provides an easy and organized way to execute one operation from multiple available options.

Definition:

An Arithmetic Operation Program accepts two numbers and a choice from the user. Depending on the selected option, the program performs addition, subtraction, multiplication, or division using the switch statement.

Syntax:

switch(choice)
{
    case 1:
        // Addition
        break;

    case 2:
        // Subtraction
        break;

    case 3:
        // Multiplication
        break;

    case 4:
        // Division
        break;

    default:
        // Invalid Choice
}

Explanation:

  • The program accepts two integer values from the user.
  • It then asks the user to select an arithmetic operation.
  • The switch statement executes the selected operation.
  • If the user selects Division, the program checks whether the second number is zero before performing the operation.
  • If an invalid choice is entered, the default case displays an error message.

Program: Arithmetic Operation Using Switch Statement

Explanation: This program accepts two numbers and performs addition, subtraction, multiplication, or division according to the user's choice using the switch statement.


#include <stdio.h>
#include <conio.h>

main()
{
    int a,b,c;

    clrscr();

    printf("Enter the value of a : ");
    scanf("%d",&a);

    printf("\nEnter the value of b : ");
    scanf("%d",&b);

    printf("\nEnter your choice");
    printf("\n1. Addition");
    printf("\n2. Subtraction");
    printf("\n3. Multiplication");
    printf("\n4. Division");

    printf("\n\nEnter Your Choice : ");
    scanf("%d",&c);

    switch(c)
    {

        case 1:
            c = a + b;
            printf("\nAnswer is %d",c);
            break;

        case 2:
            c = a - b;
            printf("\nAnswer is %d",c);
            break;

        case 3:
            c = a * b;
            printf("\nAnswer is %d",c);
            break;

        case 4:

            if(b!=0)
            {
                c = a / b;
                printf("\nAnswer is %d",c);
            }
            else
            {
                printf("\nDivision by zero is not possible.");
            }

            break;

        default:

            printf("\nInvalid Choice");

    }

    getch();

}

Output:

Enter the value of a : 20
Enter the value of b : 10
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division

Enter Your Choice : 3

Answer is 200

Practice Exercises

Task 1: Write a C program using the switch statement to calculate the area of different shapes based on the user's choice.

Goal: Learn how to use the switch statement to perform different calculations depending on the option selected by the user.
💡 Hint: Display a menu with the following options:
  • 1. Area of Square
  • 2. Area of Rectangle
  • 3. Area of Circle
Use the appropriate formula according to the user's choice. Display "Invalid Choice" if the entered option is not available.
💡 Show Solution

#include <stdio.h>
#include <conio.h>

main()
{
    int choice;
    float side,length,width,radius,area;

    clrscr();

    printf("1. Area of Square");
    printf("\n2. Area of Rectangle");
    printf("\n3. Area of Circle");

    printf("\n\nEnter Your Choice : ");
    scanf("%d",&choice);

    switch(choice)
    {

        case 1:

            printf("\nEnter Side : ");
            scanf("%f",&side);

            area = side * side;

            printf("\nArea = %.2f",area);

            break;

        case 2:

            printf("\nEnter Length : ");
            scanf("%f",&length);

            printf("Enter Width : ");
            scanf("%f",&width);

            area = length * width;

            printf("\nArea = %.2f",area);

            break;

        case 3:

            printf("\nEnter Radius : ");
            scanf("%f",&radius);

            area = 3.14 * radius * radius;

            printf("\nArea = %.2f",area);

            break;

        default:

            printf("\nInvalid Choice");

    }

    getch();

}

Output
1. Area of Square
2. Area of Rectangle
3. Area of Circle

Enter Your Choice : 1
Enter Side : 5

Area = 25.00
Explanation: The program displays a menu of different shapes. The user selects a shape by entering a choice, and the switch statement performs the corresponding area calculation. If an invalid option is entered, the program displays "Invalid Choice".