Problem 7.1,Let Us C Solution

0 min read

  Write a menu driven program which has following options:

1.Factorial of a number
2.Prime or not
3.Odd or even
4.Exit
Once a menu item is selected the appropriate action should be taken
and once this action is finished,the menu should reappear.Unless the
user selects the 'Exit'option the program should continue to work.  */
#include<stdio.h>
#include<stdlib.h>
int main()
{
	int choice,num,i,fact;
	while(1)
	{
		printf("\n1.Factorial\n");
		printf("2.Prime\n");
		printf("3.Odd/Even\n");
		printf("4.Exit\n");
		printf("Your choice?");
		scanf("%d",&choice);
		switch (choice)
		{
			case 1:
				printf("\nEnter number:");
				scanf("%d",&num);
				fact=1;
				for(i=1;i<=num;i++)
				fact=fact*i;
				printf("Factorial value=%d\n",fact);
				break;
				
				case 2:
					printf("\nEnter number:");
					scanf("%d",&num);
					for(i=2;i<num;i++)
					{
						if(num%i==0)
						{
							printf("Not a prime number\n");
							break;
						}
					}
					if(i==num)
					printf("Prime number\n");
					break;
					case 3:
						printf("\nEnter number:");
						scanf("%d",&num);
						if(num %2==0)
						printf("Even number\n");
						else 
						printf("Odd number\n");
						break;
						case 4:
							exit(0);// Terminates program execution
							default :
								printf("Wrong choice\a\n");
		}
	}
	return 0;
}
Welcome to prgrmramit.blogspot.com! I'm Amit Singh, an expert in AI, Data Science, and Machine Learning. I created this blog to share practical insights and tips for those eager to learn and gro…

You may like these posts

  •  Exercise[6B(c)]When interest compunds q times per year at an annual rate of r% for n years,the principle p compounds to an amount a as per the following formula a=p(1+r/q)^nq…
  •  Exercise[6B(f)]Population of a town today is 100000.The population hd increased steadily at the rate of 10% per year for last 10 years.Write a program to determine the popu…
  •  Exercise[6B(b)]According to a study,the approximate level of intelligence of person can be calculated using the following formula: i=2+(y+0.5x) Write a program that will pr…
  •  Exercise[6B(g)]Ramanujan number is the smallest number that can be expressed as sum of two cubes in two different ways.Write a program to print all such numbers up to a re…
  •  Exercise[6B(d)]The natural logarithm can be approximated by the following series. x-1/x+1/2(x-1/x)^2+1/2(x-1/x)^3+1/2(x-1/x)^4+... If x is input through the keyboard,write a …
  •  Exercise[6B(e)]Write a program to generate all Pythagorean Triplets with side length less than or equal to 30. */ #include<stdio.h> #include<math.h> int…

Post a Comment

Subscribe with Gmail


Premium By Raushan Design