Thursday, September 4, 2014

Write a menu driven program using switch statement which add, subtract, multiply, and divide


Problem

Write a menu driven program using switch statement (without functions) that has the following options:
  1. Add numbers
  2. Subtract numbers
  3. Multiply numbers
  4. Divide numbers
  5. Exit
The switch case statement and the menu (mentioned above) should be in while(1) loop. The only way to end the program is that the user has to selection choice 5. In case 5 you can simply call a function exit (0);

Solution - Code

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main()
{
 int i,x,y,c;
 printf("Enter two numbers : ");
 scanf("%d %d",&x,&y);
 printf("1. addition\n");
 printf("2. subtraction\n");
 printf("3. multiplication\n");
 printf("4. division\n");
 printf("5. exit\n\n");
 printf("Which action you want to perform : ");
 scanf("%d",&i);
 switch(i)
 {
  case 1:
   c = (x+y);
   printf("Answer = %d\n",c);
   break;
  case 2:
   c = (x-y);
   printf("Answer = %d\n",c);
   break;
  case 3:
   c = (x*y);
   printf("Answer = %d\n",c);
   break;
  case 4:
   c = (x/y);
   printf("Answer = %d\n",c);
   break;
  case 5 :
   exit(0);
  default :
   printf("Enter valid number\n");
 }
 system("pause");
 return 0;
}

Sample Output

switch statement in c language

No comments :

Post a Comment

Note: Only a member of this blog may post a comment.

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact