Perform arithmetic operations according to entered operator in C

Q. Write a program using switch case statement, which takes two numbers and arithmetic operator as a input, performs calculation as per chosen operator and displays the result.
==> Code goes like this : 
 /* Perform arithmetic operations using switch case */
#include<stdio.h>
int main()
{
    float num1,num2,result;
    char sign;
    printf("Enter two numbers:");
    scanf("%f %f",&num1,&num2);
    printf("Enter arithmetic operator (\+,-,*,/) :");
    scanf(" %c",&sign);
    switch(sign)
    {
        case '+':
        result=num1+num2;
        break;
              
        case '-':
        result=num1-num2;
        break;
      
        case '*':
        result=num1*num2;
        break;
      
        case '/':
        result=num1/num2;
        break;
      
        default:
        printf("\n Wrong Choice");
    }
    printf("\n The result is %.2f",result);
   
return 0;  
}


Output: Operator : +











Output: Operator : - 












Output: Operator : *
  










 Output : Operator : / 












I have used Dev CPP to compile this program.
If you are using Turbo C++ IDE, then use conio.h in link section after stdio.h
and use getch(); to hold the output screen, just before the end of main function.
If you have any confusion regarding the program, please, comment below.
Happy Coding !
Thank you
 
Share on Google Plus

About Nepali Xoro

Interested in Information Technology, Spreading Knowledge for Nepalease IT Students."Loves Traveling", Listen Music and Surfing web.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment