This program give you an idea to design your own menu driven program in C/C++. To fulfill our requirement we use Switch case for selecting one option form multiple option, function for performing individual task in which we pass parameters. It also demonstrate function with parameter with no return type.
Source Code:
#include <stdio.h>
#include <conio.h>
void sum(int, int);
void multiply(int, int);
void divide(int, int);
void subtract(int, int);
void main()
{
int opt, x, y;
while(1)
{
clrscr();
printf("Simple Menu Driven Program");
printf("\n-----------------------------\n");
printf("1. Sum\n");
printf("2. Subtract\n");
printf("3. Multiply\n");
printf("4. Divide\n");
printf("5. Exit\n");
printf("--------------------------------\n");
printf("Select option (1-5) : ");
scanf("%d", &opt);
switch(opt)
{
case 1:
{
printf("Enter Two Number: ");
scanf("%d %d", &x, &y);
sum(x,y);
break;
}
case 2:
{
printf("Enter Two Number : ");
scanf("%d %d", &x, &y);
subtract(x,y);
break;
}
case 3:
{
printf("Enter Two Number : ");
scanf("%d %d", &x, &y);
multiply(x,y);
break;
}
case 4:
{
printf("Enter Two Number : ");
scanf("%d %d", &x, &y);
divide(x,y);
break;
}
case 5:
{
exit(0);
}
default :
{
printf("Select Valid Option (1-5) :");
break;
}
}
getch();
}
}
void sum(int a, int b)
{
int sum = a+b;
printf("Sum of %d and %d is %d", a, b, sum);
}
void subtract(int a , int b)
{
int sub = a-b;
printf("Subtraction of %d and %d is %d", a, b, sub);
}
void divide(int a, int b)
{
float div = (float)a/b;
printf("Division of %d and %d is %f", a, b, div);
}
void multiply(int a, int b)
{
int mul = a*b;
printf("Multiplication of %d and %d is %d", a, b, mul);
}
#include <stdio.h>
#include <conio.h>
void sum(int, int);
void multiply(int, int);
void divide(int, int);
void subtract(int, int);
void main()
{
int opt, x, y;
while(1)
{
clrscr();
printf("Simple Menu Driven Program");
printf("\n-----------------------------\n");
printf("1. Sum\n");
printf("2. Subtract\n");
printf("3. Multiply\n");
printf("4. Divide\n");
printf("5. Exit\n");
printf("--------------------------------\n");
printf("Select option (1-5) : ");
scanf("%d", &opt);
switch(opt)
{
case 1:
{
printf("Enter Two Number: ");
scanf("%d %d", &x, &y);
sum(x,y);
break;
}
case 2:
{
printf("Enter Two Number : ");
scanf("%d %d", &x, &y);
subtract(x,y);
break;
}
case 3:
{
printf("Enter Two Number : ");
scanf("%d %d", &x, &y);
multiply(x,y);
break;
}
case 4:
{
printf("Enter Two Number : ");
scanf("%d %d", &x, &y);
divide(x,y);
break;
}
case 5:
{
exit(0);
}
default :
{
printf("Select Valid Option (1-5) :");
break;
}
}
getch();
}
}
void sum(int a, int b)
{
int sum = a+b;
printf("Sum of %d and %d is %d", a, b, sum);
}
void subtract(int a , int b)
{
int sub = a-b;
printf("Subtraction of %d and %d is %d", a, b, sub);
}
void divide(int a, int b)
{
float div = (float)a/b;
printf("Division of %d and %d is %f", a, b, div);
}
void multiply(int a, int b)
{
int mul = a*b;
printf("Multiplication of %d and %d is %d", a, b, mul);
}
Output:
No comments:
Post a Comment