English

Write a Menu Driven Program to Perform Arithmetic Operations on Two Integers. the Menu Should Be Repeated Until User Selects “Stop” Option. Program Should Have Independent User Defined Functions - Structured Programming Approach

Advertisements
Advertisements

Question

Write a menu driven program to perform arithmetic operations on two integers. The menu should be repeated until user selects “STOP” option. Program should have independent user defined functions for each case.

Answer in Brief

Solution

#include<stdio.h>
#include<conio.h>
void main()
{
int operator,ans;
double firstNumber,secondNumber;
clrscr();
do
{
printf("Enter a operation:\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Divison");
scanf("%d", &operator);
printf("Enter two operands: ");
scanf("%lf %lf",&firstNumber, &secondNumber);
switch(operator)
{
case 1:
printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber + secondNumber);
break;
case 2:
printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber - secondNumber);
break;
case 3:
printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber * secondNumber);
break;
case 4:
printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber / secondNumber);
break;
default:
printf("Error! operator is not correct");
}
printf("\nDo you want to continue 1.YES 2. NO");
scanf("%d",&ans);
}
while(ans==1);
getch();
}

Output:
Enter an Operation:
1. Addition
2. Subtraction
3. Multiplication
4. Division
       1
       Enter two operands 10 20
       10.0 + 20.0 = 30.0
       Do you want to continue 1.YES 2.NO 1
       Enter an operation
       1. Addition
       2. Subtraction
       3. Multiplication
       4. Division
       2
       Enter two operands: 50 60
       50.0 – 60.0 = -10.0
       Do you want to continue 1.YES 2.NO 2

shaalaa.com
Operators in C-Programming
  Is there an error in this question or solution?
2016-2017 (June) CBCGS

APPEARS IN

Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×
Our website is made possible by ad-free subscriptions or displaying online advertisements to our visitors.
If you don't like ads you can support us by buying an ad-free subscription or please consider supporting us by disabling your ad blocker. Thank you.