Switch-Case Structure(Programme in C)

                  Switch-Case Structure(Programme in C)

//title:     Switch-Case Structure

//brief:    this program is of performs simple arithmetic calculations.

//          it prints menu of possible operation choice,

//          asks user to choose one of the possible operations,

//          performs any one operation for valid choice,

//          and prints error message for invalid choice.

//result:   1. Add

//          2. Sub

//          3. Mul

//          4. Div

//          Enter your choice: 1

//          Enter two integers: 9 21

//          ANSWER = 30

 

#include <stdio.h>

#include <stdlib.h>

//start of main function

int main()

{

    //variable declaration

int choice, a, b;

    //print menu

    printf("1. Add\n");

    printf("2. Sub\n");

    printf("3. Mul\n");

    printf("4. Div\n");

    //get operation choice

    printf("Enter your choice: ");

    scanf("%d", &choice);

    //get two integer numbers

    printf("Enter two integers: ");

    scanf("%d %d", &a, &b);

    /*Performs operation of users choice*/

switch (choice)

    {

case 1:

        //perform addition and print answer

        printf("ANSWER = %d\n", a + b);

break;

case 2:

        //perform subtraction and print answer

        printf("ANSWER = %d\n", a - b);

break;

case 3:

        //perform multiplication and print answer

        printf("ANSWER = %d\n", a * b);

break;

case 4:

        //perform division and print answer

        printf("ANSWER = %d\n", a / b);

break;

default:

        //print error message

        printf("Error: You have entered wrong choice");

break;

    }

    //terminate main function and return 0 as output

return 0;

}

Comments

Popular posts from this blog

What is chopper

What is Resistor

Top 5 best Ethical hacking book for beginners