[C] Write a menu driven program which has following options:
1. Factorial of a number.
2. Prime or not
3. Odd or even
4. Exit
Make use of switch statement.
#include<stdio.h>
#include<conio.h>
main()
{
int choice,fact,num,i;
clrscr();
printf("Choose one of the following option:\n1. Factorial\n2. Prime or Composite\n3. Odd or Even\n4. Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter any integer: ");
scanf("%d",&num);
for(i=num,fact=1;num>1;num--)
fact= fact * num;
printf("\n%d! = %d",i,fact);
break;
case 2:
printf("\nEnter any integer: ");
scanf("%d",&num);
for(i=num-1;i>0;--i)
{
if(num%i==0 && i!=1)
{
printf("\nComposite Number");
break;
}
else
{
if(i==1)
printf("\nPrime Number");
else
continue;
}
}
break;
case 3:
printf("\nEnter any integer: ");
scanf("%d",&num);
((num%2==0)?printf("\nEven Number"):printf("\nOdd Number"));
break;
case 4:
break;
default:
printf("\nInvalid Selection!! Please try again.");
}
getch();
}
#include<conio.h>
main()
{
int choice,fact,num,i;
clrscr();
printf("Choose one of the following option:\n1. Factorial\n2. Prime or Composite\n3. Odd or Even\n4. Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter any integer: ");
scanf("%d",&num);
for(i=num,fact=1;num>1;num--)
fact= fact * num;
printf("\n%d! = %d",i,fact);
break;
case 2:
printf("\nEnter any integer: ");
scanf("%d",&num);
for(i=num-1;i>0;--i)
{
if(num%i==0 && i!=1)
{
printf("\nComposite Number");
break;
}
else
{
if(i==1)
printf("\nPrime Number");
else
continue;
}
}
break;
case 3:
printf("\nEnter any integer: ");
scanf("%d",&num);
((num%2==0)?printf("\nEven Number"):printf("\nOdd Number"));
break;
case 4:
break;
default:
printf("\nInvalid Selection!! Please try again.");
}
getch();
}
[D] Write a program which to find the grace marks for a student using switch. The user should enter the class obtained by the student and the number of subjects he has failed in.
-If the student gets first class and the number of subjects he failed in is greater than 3, then he does not get any grace. If the number of subjects he failed in is less than or equal
to 3 then the grace is of 5 marks per subject.
- If the student gets second class and the number of subjects he failed in is greater than 2, then he does not get any grace. If the number of subjects he failed in is less than or
equal to 2 then the grace is of 4 marks per subject.
- If the student gets third class and the number of subjects he failed in is greater than 1, then he does not get any grace. If the number of subjects he failed in is equal to 1
then the grace is of 5 marks per subject
#include<stdio.h>
#include<conio.h>
main()
{
int clas,n_o_s; //clas=class,n_o_s=no of subjects in which student fails
clrscr();
printf("Enter the class obtained by the student : ");
scanf("%d",&clas);
printf("Enter the number of subjects he has failed in : ");
scanf("%d",&n_o_s);
switch(clas)
{
case(1):
if(n_o_s>3)
printf("\nStudent does not get any grace marks.");
else if(n_o_s<=3)
printf("\nStudent got 5 grace marks.");
break;
case(2):
if(n_o_s>2)
printf("\nStudent does not get any grace marks.");
else if(n_o_s<=2)
printf("\nStudent got 4 grace marks.");
break;
case(3):
if(n_o_s>1)
printf("\nStudent does not get any grace marks.");
else if(n_o_s==1)
printf("\nStudent got 5 grace marks.");
break;
default:
printf("\nPlease enter class between 1 to 3");
}
getch();
}
#include<conio.h>
main()
{
int clas,n_o_s; //clas=class,n_o_s=no of subjects in which student fails
clrscr();
printf("Enter the class obtained by the student : ");
scanf("%d",&clas);
printf("Enter the number of subjects he has failed in : ");
scanf("%d",&n_o_s);
switch(clas)
{
case(1):
if(n_o_s>3)
printf("\nStudent does not get any grace marks.");
else if(n_o_s<=3)
printf("\nStudent got 5 grace marks.");
break;
case(2):
if(n_o_s>2)
printf("\nStudent does not get any grace marks.");
else if(n_o_s<=2)
printf("\nStudent got 4 grace marks.");
break;
case(3):
if(n_o_s>1)
printf("\nStudent does not get any grace marks.");
else if(n_o_s==1)
printf("\nStudent got 5 grace marks.");
break;
default:
printf("\nPlease enter class between 1 to 3");
}
getch();
}