Thursday, September 4, 2014

create a menu driven program to add, subtract, multiply, and divide two number using functions and switch case statement


Problem

Write a menu driven program using switch statement that has the following options:
  1. Add numbers
  2. Subtract numbers
  3. Multiply numbers
  4. Divide numbers
  5. Exit
The prototype of the function for add, subtract, multiply and divide is as follows:
int add(int, int);
int sub(int, int);
int mul(int, int);
int div(int, int);

Solution - Code

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

int add (int, int);
int sub (int, int);
int mul (int, int);
int divi (int, int);

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=add(x , y);
   printf("Answer = %d\n",c);
   system("pause");
   break;
  case 2:
   c=sub(x , y);
   printf("Answer = %d\n",c);
   system("pause");
   break;
  case 3:
   c=mul(x , y);
   printf("Answer = %d\n",c);
   system("pause");
   break;
  case 4:
   c=divi(x,y);
   printf("Answer = %d\n",c);
   system("pause");
   break;
  case 5 :
   exit(0);
   break;
  default :
   printf("Enter valid number\n");
   system("pause");
 }
}
int add(int x, int y)
{
 int c;
 c = (x+y);
 return c ;
}
int sub(int x, int y)
{
 int c;
 c = (x-y);
 printf("Answer = %d\n",c);
}
int mul(int x, int y)
{
 int c;
 c = (x*y);
 printf("Answer = %d\n",c);
}
int divi(int x, int y)
{
 int c;
 c = (x/y);
 printf("Answer = %d\n",c);
}

Sample Output

switch case statement

1 comment :

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

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