Menu driven program - calculates factorial, detects a prime number, even/odd

Write a menu driven program which has the following options:
1. Factorial of a Number
2. Prime or Not
3. Odd or Even
4. Exit

Make use of a switch Statement.


Outline of the program:

//a menu driven program
main()
{
int choice;
while (1)
{
printf("\n1. Factorial");
printf("\n2. Prime");
printf("\n3. Odd/ Even");
printf("\n4. Exit");
printf("\n Your Choice? : ");
scanf ("%d", &choice);

switch (choice)
{
case 1:
//logic for factorial of a number
break;
case 2:
//logic for deciding a prime number
case 3:
//logic for odd/even
break;
case 4:
exit();
}
}
}


NOTES:
1) The statement while(1) puts the entire logic in an infinite loop. This is necessary
since the menu must keep reappearing on the screen once an item is selected and an
appropriate action is taken.
2) exit() is a standard library function. When it gets executed the program execution is
immediately terminated. Note that a break would not have worked here because it would have
taken control only outside the switch and not outside the while.

#include<stdio.h>


main()

{
int choice, factorial, temp, number, i;
//The while(1) is needed so that the program runs in an infinite loop!

while (1)
{
printf("\n1. Factorial");

printf("\n2. Prime");
printf("\n3. Odd/ Even");
printf("\n4. Exit");

printf("\n\nYour Choice? : ");
scanf ("%d", &choice);

switch (choice)
{
case 1:

//The following snippet calculates the factorial of a number.
printf("\nEnter the Number:");

scanf ("%d", &number);
for (temp=1,factorial=1; temp<=number;temp++)

{
factorial=factorial*temp;
continue;
}

printf("Factorial=%d\n\n",factorial);
break;

case 2:

//The following snippet detects a prime number.
printf("Enter the Number:");

scanf ("%d", &number);

if (number==1)

{
printf("The Number 1 is a Unique Number.\n\n ");
}

for (i=2; i<=number-1; i++)

{
if (number%i==0)
{

printf("Number is not prime\n\n");
break;
}


if (number==i || number%i !=0)

{
printf("Number is Prime\n\n");
break;
}


}

break; //break out of this case.



case 3:
//The following snippet detects Even/Odd Numbers.
printf("Enter the Number:");

scanf ("%d", &number);
if (number%2==0)

{
printf("The number is Even\n\n");
}
else

{
printf("The number is Odd\n\n");
}
break;


case 4:
//The Following command comes out of the program.
exit();


default:
printf("You have entered the wrong choice. Please try again. \n");
break;
}

}
}


The file can be found at:
Download File

Comments

surya said…
become a follower for free C,C++ And vb code of my blog:
http://www.a2zhacks.blogspot.com to learn C,C++,VB programming.
Anonymous said…
If program is showing an error for the exit function then you can add a header file #include and the exit function should be exit(0)
Anonymous said…
#include
Anonymous said…
stdlib.h in header file
Anonymous said…
can anyone pls look at my work.. im doing a menu program.. i can't run the program.. the .system' can't identify..


#include
#include

int printMenu()

{
system("clear");

int response;
fprintf(stdout,"**************************************\n");
fprintf(stdout," Y O S H I N O Y A \n");
fprintf(stdout,"************************************\n\n");
fprintf(stdout,"M E N U:\n\n");
fprintf(stdout," 1. Gyudon Beef P145.00\n");
fprintf(stdout," 2. Chicken Teriyaki P150.00\n");
fprintf(stdout," 3. Pork Katsudon P135.00\n");
fprintf(stdout," 4. Beef Yakiniku P135.00\n");
fprintf(stdout," 5. Beef Misono P135.00\n");
fprintf(stdout," 6. Chicken Katsudon P135.00\n");
fprintf(stdout," 7. Italian Beef Bowl P120.00\n");
fprintf(stdout," 8. Ebi Ramen P120.00\n");
fprintf(stdout," 9. Yaki Udon P80.00\n");
fprintf(stdout," 10.Red tea P45.00\n");
fprintf(stdout,"Type F when you finished with your selections.\n\n");
fprintf(stdout,"************************************\n\n");
fprintf(stdout,"Select 1, 2, 3, 4, 5, 6, 7, 8, 9, or 10 --->\n\n");
fscanf(stdin, "%d", &response);
return response;
}

int menu()

{
int s;
float p, x;
int response;

response = printMenu();
printf("How many orders of item number %d would you like?\n", response),
scanf("%d", &s);

switch(response)
{
printf("You have ordered %d order(s) 1. Gyudon Beef\n", s);
p=s*145;
printf("Your total is %.2f\n\n", p);
break;
printf("You have ordered %d order(s) 2. Chicken Teriyaki\n", s);
p=s*150;
printf("Your total is %.2f\n\n", p);
break;
printf("You have ordered %d order(s) 3. Pork Katsudon\n", s);
p=s*135;
printf("Your total is %.2f\n\n", p);
break;
printf("You have ordered %d order(s) 4. Beef Yakiniku\n", s);
p=s*135;
printf("Your total is %.2f\n\n", p);
break;
printf("You have ordered %d order(s) 5. Beef Misono\n", s);
p=s*135;
printf("Your total is %.2f\n\n", p);
break;
printf("You have ordered %d order(s) 6. Chicken Katsudon\n", s);
p=s*135;
printf("Your total is %.2f\n\n", p);
break;
printf("You have ordered %d order(s) 7. Italian Beef Bowl\n", s);
p=s*120;
printf("Your total is %.2f\n\n", p);
break;
printf("You have ordered %d order(s) 8. Ebi Ramen\n", s);
p=s*120;
printf("Your total is %.2f\n\n", p);
break;
printf("You have ordered %d order(s) 9. Yaki Udon\n", s);
p=s*80;
printf("Your total is %.2f\n\n", p);
break;
printf("You have ordered %d order(s) 10. Red Tea\n", s);
p=s*45;
printf("Your total is %.2f\n\n", p);
default:
break;

}
x=p*1.13;
printf("Please pay %.2f\n\n", x);
printf("Thank you for eating at Y O S H I N O Y A!\n\n");
}
Anonymous said…
in the above given example, prime number code is wrong...

Popular posts from this blog

C Program - Calculation of Area and Circumference of a Circle using Pointers

Matchstick Game using C Programming

C Program to Calculate Factorial of a number using Recursion