Class 9th CBSE

Q.2 Write a menu driven program that performs arithmetic operations (+, – , *, /, %) according to the user’s choice. 

Program 2

Q.2 Write a menu driven program that performs arithmetic operations (+, - , *, /, %) according to the user's choice. 

Solution :- 

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
int op;
printf(“Press 1 for Addition \n Press 2 for Substaction \n Press 3 for Mutiplication\n Press 4 for Division \n Pess 5 for Modules “);
printf(“\nEnter the First number :- “);
scanf(“%d”,&a);
printf(“Enter the Second number :- “);
scanf(“%d”,&b);
printf(“Enter the Choice :- “);
scanf(“%d”,&op);
switch(op)
{
case 1 :
printf(“Sum of %d and %d is : %d”,a,b,a+b);
break;
case 2 :
printf(“Difference of %d and %d is : %d”,a,b,a-b);
break;
case 3 :
printf(“Multiplication of %d and %d is : %d”,a,b,a*b);
break;
case 4 :
printf(“Division of Two Numbers is %d : “,a/b);
break;
default :
printf(” Enter Your Correct Choice.”);
break;
}
getch();
}

 

Q.2 Write a menu driven program that performs arithmetic operations (+, - , *, /, %) according to the user's choice. (Algorithm)
 

Solution :- 

1. Start
2. Declare variables: num1, num2, result, and choice
3. Display “Menu:”
4. Display “1. Addition (+)”
5. Display “2. Subtraction (-)”
6. Display “3. Multiplication (*)”
7. Display “4. Division (/)”
8. Display “5. Modulus (%)”
9. Display “6. Exit”
10. Input choice
11. switch (choice):
12. case 1:
13. Input num1 and num2
14. result = num1 + num2
15. Display “Result: %f”, result
16. break
17. case 2:
18. Input num1 and num2
19. result = num1 – num2
20. Display “Result: %f”, result
21. break
22. case 3:
23. Input num1 and num2
24. result = num1 * num2
25. Display “Result: %f”, result
26. break
27. case 4:
28. Input num1 and num2
29. if (num2 != 0):
30. result = (float)num1 / num2
31. Display “Result: %f”, result
32. else:
33. Display “Division by zero is not allowed.”
34. break
35. case 5:
36. Input num1 and num2
37. if (num2 != 0):
38. result = num1 % num2
39. Display “Result: %d”, result
40. else:
41. Display “Modulus by zero is not allowed.”
42. break
43. case 6:
44. Display “Exiting the program.”
45. break
46. default:
47. Display “Invalid choice. Please select a valid option (1-6).”
48. End

Q.2 Write a menu driven program that performs arithmetic operations (+, - , *, /, %) according to the user's choice. (Flow Chart)
 

Solution :- 

 

Fun & Easy to follow
Works on all devices
Your own Pace
Super Affordable

Popular Videos

Play Video

UX for Teams

Learn the basics and a bit beyond to improve your backend dev skills.

ava4.png
Chris Matthews

Designer

Play Video

SEO & Instagram

Learn the basics and a bit beyond to improve your backend dev skills.

ava4.png
Chris Matthews

Designer