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
//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
No comments:
Post a Comment
kiss on google ads if you are anonymous because your ip is trackable.thank you.
......from.admin