Introduction :
The menu driven program is a common program in the programming world. There are lists of options in a menu driven program. From the options, user can select an option to perform a specific task or operation. Creating an all in one menu driven program in C or C++ graphics programming language is an interesting and educational project for developing.
Menu driven program gives a user-friendly interface by which user can perform various tasks (mathematical and logical operations) based on his input in the menu system. In this article, I shall explore you how to create an all in one menu driven program using C or C++ graphics programming language. Here, I have built the menu driven program in the Turbo C++ IDE using graphics.h library.
About the program :
This is a simple menu driven graphics program where you can calculate various mathematical and logical operations based on user input through a menu system. When you run the program, you will see a menu with various options for mathematical and logical operations. Now you have to enter a choice. According to your input, the program executes the corresponding operation.
In the program, you can perform addition, subtraction, multiplication and division of numbers. Here, you also see a calculator option in the program. You can also calculate various tasks such as simple interest, gross salary, distance conversion, total marks and percentage, temperature conversion and more.
After completing an operation, the program asks you to press any key to display the menu again. When you enter an incorrect choice, you can hear an alert sound. To stop the program, you have to enter “0”.
Explanation of the program :
At first, include header files such as graphics.h, stdlib.h, stdio.h, conio.h, dos.h and math.h in the program. In the main function, declare different types of various variables. Using initgraph() function initialize the graphics system with “graphic_driver” and “graphic_mode”. Now, set the background and foreground colors using setbkcolor() and setcolor() functions respectively.
In the infinite while loop, display a menu with options for various mathematical and logical operations using printf() function. Now, you can take the user choice as input by scanf() function. Then clear the screen using system() function. After that, you can use a switch case statement to execute different operations based on the input of the user.
In the switch case statement, your choice must be 0 to 22. Otherwise, your input will be wrong. When you enter your choice, the switch case statement performs appropriate task according to your choice. If you enter “0”, the program will be stopped because using “goto last” exit the infinite loop and close the graphics system by closegraph() function. When you enter 1 to 22 as choice, you can calculate different task according to your choice.
If you enter wrong or invalid choice (except 0 to 22), the program produces an error sound using sound() and nosound() function.
How run the program :
To run the program on your pc, you have to install the Turbo C++ IDE. After that, create a C or C++ file in the Turbo C++ IDE with .c or .cpp extension. Now, copy the following source code of menu driven program and paste in the C or C++ file. Do you know how to copy paste in the Turbo C++ IDE? You can also know how to use graphics.h in Turbo C++ IDE from here.
Source code :
The following code is the source code of the menu driven program in C or C++ programming language.
/*Developed by Puskar Jasu*/
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <math.h>
int main(void)
{
int choice, a, b, i, n, s = 0, m = 1, num[100], big = -32000, small = 32000;
float r, p;
char ch;
int graphic_driver = DETECT, graphic_mode;
initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
setbkcolor(4);
setcolor(14);
while (1)
{
setbkcolor(4);
printf("0.EXIT\n1.ADD\n2.SUBTRACT\n3.MULTIPLY\n4.DIVISION\n5.CALCULATOR\n");
printf("6.SIMPLE INTEREST\n7.GROSS SALARY\n8.CONVERT DISTANCE\n");
printf("9.TOTAL MARKS AND PERCENTAGE OF STUDENT\n10.TEMPERATURE CONVERT\n");
printf("11.AREA AND PERIMETER OF RECTANGLE\n12.AREA AND CIRCUMFERENCE");
printf("OF CIRCLE\n13.AREA OF TRIANGLE\n14.SUM OF DIGITS OF NUMBER\n");
printf("15.REVERSE A NUMBER\n16.INTERCHANGE TWO NUMBER\n17.PROFIT ");
printf("OR LOSS\n18.EVEN OR ODD\n19.LEAP YEAR OR NOT\n20.PRIME OR NOT\n");
printf("21.FIND DAY FROM DATE\n");
printf("22.FIND SMALLEST AND BIGEST FRIEND\n");
printf("ENTER YOUR CHOICE\n");
fflush(stdin);
scanf("%d", &choice);
system("cls");
setbkcolor(4);
setcolor(14);
switch (choice)
{
case 0:
goto last;
case 1:
printf("\nENTER HOW MANY NUMBER YOU WANT TO ADD\n");
scanf("%d", &n);
printf("\nENTER %d NUMBER\n", n);
for (i = 0; i <= n - 1; i++)
{
scanf("%d", &num[i]);
s = s + num[i];
}
printf("\nSUM=%d", s);
break;
case 2:
printf("ENTER TWO NUMBER FOR SUB\n");
scanf("%d%d", &a, &b);
printf("\nSUB = %d", a - b);
break;
case 3:
printf("\nENTER HOW MANY NUMBER YOU WANT TO MULTIPLY\n");
scanf("%d", &n);
printf("\nENTER %d NUMBER\n", n);
for (i = 0; i <= n - 1; i++)
{
scanf("%d", &num[i]);
m = m * num[i];
}
printf("\nMULTIPLY=%d", m);
break;
case 4:
printf("ENTER TWO NUMBER FOR DIVISION\n");
scanf("%d%d", &a, &b);
printf("\nDIVISION= %0.2f", (a * 1.0) / b);
break;
case 5:
while (1)
{
printf("\nCALCULATOR\nPRESS 0 FOR STOP\n");
printf("enter any number\n");
scanf("%f", &r);
if (r == 0)
break;
r = r * 1.0;
fflush(stdin);
printf("enter + for add, - for sub, * for multiply, / for division\n");
scanf("%c", &ch);
printf("enter another number\n");
scanf("%f", &p);
if (ch == '+')
r = r + p;
if (ch == '-')
r = r - p;
if (ch == '*')
r = r * p;
if (ch == '/')
r = r / p;
printf("%0.2f\n", r);
}
break;
case 6:
printf("\nENTER PRINCIPAL, RATE AND YEAR\n");
scanf("%f%f%d", &p, &r, &n);
printf("\nSIMPLE INTEREST= %0.2f", p * n * r / 100.0);
break;
case 7:
printf("\nENTER BASIC SALARY, D.A. AND H.R.A OF PERSON\n");
scanf("%d%d%d", &a, &b, &n);
printf("\nGROSS SALARY= %0.2f", a + a * b / 100.0 + a * n / 100.0);
break;
case 8:
printf("\nENTER DISTANCE IN K.M\n");
scanf("%f", &p);
printf("\nDISTANCE IN METER= %0.2f", 1000.0 * p);
printf("\nDISTANCE IN SENTIMETER= %0.2f", 100000.0 * p);
printf("\nDISTANCE IN FEET= %0.2f", 1000.0 * p * 3.28);
printf("\nDISTANCE IN INCHES= %0.2f", 1000.0 * p * 3.28 * 12);
break;
case 9:
printf("\nENTER HOW NANY SUBJECT IN THE EXAM.\n");
scanf("%d", &n);
printf("\nENTER MARKS OF %d SUBJECT\n", n);
for (i = 0; i <= n - 1; i++)
{
scanf("%d", &num[i]);
}
for (i = 0; i <= n - 1; i++)
{
s = s + num[i];
}
p = s * 1.0 / n;
printf("\nTOTAL MARKS= %d AND PERCENTAGE= %0.2f", s, p);
break;
case 10:
printf("\nENTER TEMPERATURE IN FAHRENHEIT\n");
scanf("%f", &p);
printf("\nTEMPERATURE IN CENTIGRADE IS %0.2f", (p - 32) * 5 / 9);
break;
case 11:
printf("\nENTER LENGTH AND BREADTH OF RECTANGLE\n");
scanf("%d%d", &a, &b);
printf("\nAREA OF RECTANGLE= %d AND PERIMETER OF RECTANGLE= %d", a * b, 2 * (a + b));
break;
case 12:
printf("\nENTER RADIUS OF CIRCLE\n");
scanf("%d", &a);
printf("\nAREA OF CIRCLE= %0.2f AND CIRCUMFERENCE OF CIRCLE= %0.2f", 22.0 / 7 * a * a, 2 *
22.0 / 7 * a);
break;
case 13:
printf("\nENTER THREE SIDE OF TRIANGLE\n");
scanf("%d%d%d", &a, &b, &n);
s = (a + b + n) / 2;
p = sqrt(s * (s - a) * (s - b) * (s - n));
printf("\nAREA OF TRIANGLE= %0.2f", p);
break;
case 14:
printf("\nENTER A NUMBER\n");
scanf("%d", &a);
while (a != 0)
{
b = a % 10;
a = a / 10;
s = s + b;
}
printf("\nSUM OF DIGITS= %d", s);
break;
case 15:
printf("\nENTER A NUMBER\n");
scanf("%d", &a);
printf("\nREVERSE THE NUMBER= ");
while (a != 0)
{
b = a % 10;
a = a / 10;
printf("%d", b);
}
break;
case 16:
printf("\nENTER TWO NUMBER FOR INTERCHANGE\n");
scanf("%d%d", &a, &b);
n = a;
a = b;
b = n;
printf("\nAFTER INTERCHANGE NUMBER ARE %d AND %d", a, b);
break;
case 17:
printf("\nENTER COST PRICE AND SELLING PRICE\n");
scanf("%d%d", &a, &b);
if (b > a)
printf("\nPROFIT= %d", b - a);
else if (a > b)
printf("\nLOSS= %d", a - b);
else if (a == b)
printf("\nNO PROFIT NO LOSS");
break;
case 18:
printf("\nENTER A NUMBER\n");
scanf("%d", &a);
if (a % 2 == 0)
printf("\nTHE NUMBER IS EVEN");
else
printf("\nTHE NUMBER IS ODD");
break;
case 19:
printf("\nENTER A YEAR\n");
scanf("%d", &a);
if ((a % 100 == 0 && a % 400 == 0) || (a % 100 != 0 && a % 4 == 0))
printf("\nTHE YEAR IS LEAP YEAR");
else
printf("\nTHE YEAR IS NOT LEAP YEAR");
break;
case 20:
printf("\nENTER A NUMBER\n");
scanf("%d", &a);
i = 2;
while (i <= a - 1)
{
if (a % i == 0)
{
printf("\nTHE NUMBER IS NOT PRIME");
break;
}
i++;
}
if (a == i)
printf("\nTHE NUMBER IS PRIME");
break;
case 21:
printf("\nENTER YEAR\n");
scanf("%d", &a);
printf("\nENTER MONTH\n");
scanf("%d", &b);
printf("\nENTER DAYS\n");
scanf("%d", &n);
if (((a % 100 == 0) && (a % 400 == 0)) || ((a % 100 != 0) && (a % 4 == 0)))
s = 29;
else
s = 28;
if (b == 1)
m = 0;
else if (b == 2)
m = 31;
else if (b == 3)
m = 31 + s;
else if (b == 4)
m = 31 + s + 31;
else if (b == 5)
m = 31 + s + 31 + 30;
else if (b == 6)
m = 31 + s + 31 + 30 + 31;
else if (b == 7)
m = 31 + s + 31 + 30 + 31 + 30;
else if (b == 8)
m = 31 + s + 31 + 30 + 31 + 30 + 31;
else if (b == 9)
m = 31 + s + 31 + 30 + 31 + 30 + 31 + 31;
else if (b == 10)
m = 31 + s + 31 + 30 + 31 + 30 + 31 + 31 + 30;
else if (b == 11)
m = 31 + s + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
else if (b == 12)
m = 31 + s + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
i = ((a - 1) + (a - 1) / 4 + (a - 1) / 400 - (a - 1) / 100 + m + n) % 7;
if (i == 1)
printf("\nMONDAY");
else if (i == 2)
printf("\nTUESDAY");
else if (i == 3)
printf("\nWEDNESDAY");
else if (i == 4)
printf("\nTHUSDAY");
else if (i == 5)
printf("\nFRIDAY");
else if (i == 6)
printf("\nSATURDAY");
else
printf("\nSUNDAY");
break;
case 22:
printf("\nENTER HOW MANY FRIEND\n");
scanf("%d", &n);
printf("\nENTER AGE OF %d FRIEND", n);
for (i = 0; i <= n - 1; i++)
scanf("%d", &num[i]);
for (i = 0; i <= n - 1; i++)
{
if (num[i] > big)
big = num[i];
if (num[i] < small)
small = num[i];
}
printf("\nBIG= %d AND SMALL= %d", big, small);
break;
default:
sound(800);
delay(50);
nosound();
printf("\nYOU ENTER WRONG CHOICE");
}
a = 0;
b = 0;
i = 0;
n = 0;
s = 0;
m = 1;
r = 0.0;
p = 0.0;
big = -32000;
small = 32000;
printf("\nENTER ANY KEY FOR MENU\n");
getch();
}
last:
closegraph();
return 0;
}
Output :
You can see the output of a menu driven program like below image.
Conclusion :
This is a basic menu driven program in C or C++ programming language. After completing the article, you can create your own menu driven program by adding more features based on your specific requirements. Thanks you for visiting my site.