Exercise [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
Solution:
#include<stdio.h>
#include<conio.h>
main() {
int num,i,j=0,k=0,choice,fact=1;
clrscr();
printf("Please enter any number: ");
scanf("%d",&num);
while(1)
{
printf("\n\n1. Factorial");
printf("\n2. Prime");
printf("\n3. Odd/Even");
printf("\n4. Exit");
printf("\n\nPlease enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
for(i=num;i>=1;i--) {
fact=fact*i;
}
printf("\nFactorial = %d ",fact);
break;
case 2:
for(i=2;i<num;i++) {
j=num%i;
if(j==0){
k=1;
break;
}
}
if(k==0) {
printf("\nPrime Number");
}
else {
printf("\nNot a Prime Number");
}
break;
case 3:
if((num%2)==0)
printf("\nEven Number");
else
printf("\nOdd Number");
break;
case 4:
exit();
}
}
}
------------------------------------------------------------------------------------------------------------
Exercise [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
Solution:
#include<stdio.h>
#include<conio.h>
main() {
int _class,f_sub;
clrscr();
printf("\nPlease enter the class obtained\n1=first 2=second 3=third: ");
scanf("%d",&_class);
printf("\n\nPlease enter the number of failed subjects: ");
scanf("%d",&f_sub);
switch(_class) {
case 1:
if(f_sub<=3) {
printf("\nGrace marks = 5 marks per subject.\n");
}
else {
printf("\nNo Grace marks.\n");
}
break;
case 2:
if(f_sub<=2) {
printf("\nGrace marks = 4 marks per subject.\n");
}
else {
printf("\nNo Grace marks.\n");
}
break;
case 3:
if(f_sub==1) {
printf("\nGrace marks = 5 marks per subject.\n");
}
else {
printf("\nNo Grace marks.\n");
}
break;
default:
printf("Error! wrong input.\n");
break;
}
getch();
return 0;
}
_____________________________________________________________________