C program for Calculate total using switch case

Program that asks user an arithmetic operator ('+','-','*'or '/') and two operands and perform  the corresponding calculation on the operands using switch case.                   



Code:

#include <stdio.h>
int main()
{
float a,b,res;
char choice;
printf("\n'+':Addition\n'-':Substraction\n'*':Multiply\n'/':Division\n");
printf("Enter  Your Choice Operator:");
scanf("%c",&choice);
printf("Enter 1st Number:");
scanf("%f",&a);
printf("Enter 2nd Number:");
scanf("%f",&b);
switch(choice)
{
    case '+':
    res=a+b;
    printf("The Sum of two Number=%f",res);
    break;
    case '-':
    res=a-b;
    printf("The Substraction of two Number=%f",res);
    break;
    case '*':
    res=a*b;
    printf("The Multiplication of two Numbers=%f",res);
    break;
    case '/':
    res=a/b;
    printf("The Divison of two Numbers=%f",res);
    break;
    default:
    printf("Invalid Choice");
    break;
}
return 0;
}


 

Sample Input and Output: 

'+':Addition                                                                                                                         
'-':Substraction                                                                                                                     
'*':Multiply                                                                                                                         
'/':Division                                                                                                                         
Enter  Your Choice Operator:/ 
Enter 1st Number:21                                                                                                                  
Enter 2nd Number:4                                                                                                                   
The Divison of two Numbers=5.250000          

In this article we are going to see how to write program perform different arithmetic operation like addition,multiplication,division and subtraction. So Let's understand line by line.

In the first line of code,we have included stdio.h header file which we usually do,it will help to use some of the built-in function like printf() and scanf() functions,this two function will help us for printing output and taking input on console and to console respectively.

In the next line of code,we have defined our main function with return type int,it is only point of c program from which the execution of our program will start.In the first line of our main function we have declared three variable a,b and res of type float. Those who don't known what is the use of float data type then I would like to tell them that we use this data type when we want to store decimal type value for eg 94.23,488.22,3.14 etc.a will be used to take first number as input and b will be used to store second user input and res variable will be used to store the result of a & b.

In the next line of code,we have declared one variable of type char(Character) which we will use to take operation input i.e. which operation we have to perform like  addition,multiplication etc.since the operator(+,-,/,*) is noting but a character therefore we will use char data type.After that we have written one print as shown below

printf("\n'+':Addition\n'-':Substraction\n'*':Multiply\n'/':Division\n"); 

The above statement will be evaluated as following since we have used after operation name \n(new line) Therefore everything will come on newline.

'+':Addition                                                                                                  

'-':Substraction                                                                                                                     

'*':Multiply                                                                                                                         

'/':Division      

In the next line of code,we have printed one more statement "Enter  Your Choice Operator:" it will help to understand user that we have to provide some input then only  it will proceed since we have used scanf() function immediately after it which will actually pause the execution unless and until the input is provided and %c format specifier because we are storing the input in choice which is variable of type char(Character) and we known that we use %d format specifier for integer,%c format specifier for character ,%f format specifier for float and %ld or %lf for double.

In the next line of code,we have printed one statement to ask the first number from user i.e. "Enter 1st Number:" and used scanf("%f",&a); to take user input program  will not proceed unless and until some input is provided,here we used %f format specifier this is because we are storing the input in variable a and it is of type float. 

In the next line we are doing exactly same thing for variable b which we have done for a,and after that we have defined switch case in which choice variable used as a expression.

In the next line,we have defined our first case value '+',for second case the value is '-',the third case is '*',similarly for fourth case  we have used '/'(Divide) as value.

Let's understand the whole flow with sample input

'+':Addition                                                                                  '-':Substraction                                                                              '*':Multiply                                                                                  '/':Division                                                                                  

                              

Enter  Your Choice Operator:* 

//The above option are available for us and we want to select one out of them.Let's select * 

Enter 1st Number:58  //Once we have selected operation then we have to provide first value as a input.Let's say 58

                                                                                             

Enter 2nd Number:4 // Now provide the second value,Let's say                                                                                                              

The Divison of two Numbers=232.000000



If you any have doubt,suggestion regarding this post or website then feel free to share with us.

 
If you have learned something new from this post then share this post with your family and
friends.
 

Happy Learning :)😎   

 Previous Next

Post a Comment

Previous Post Next Post