Chapter 4: Case Control Structure (Let Us 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
- 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
1. Factorial of a number.
2. Prime or not
3. Odd or even
4. Exit
// Let Us C (Chapter 4 : Case Control Structure) : Program-1
/* Write a menu driven program which has following options:
1. Factorial of a number.
2. Prime or not
3. Odd or even
4. Exit*/
#include<stdio.h>
void main()
{
int choice,N,Fact,i;
clrscr();
while(1)
{
printf("\n------------------------------------------------------");
printf("\nChoices are as below\n");
printf("1:Factorial\t");
printf("2:Prime\t\t");
printf("3:Odd/Even\t");
printf("4:Exit");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1 :
printf("Enter the No.: ");
scanf("%d",&N);
Fact=1;
i=N;
while(i>1)
{
Fact=Fact*i;
i--;
}
printf("\nFactorial of %d is %d",N,Fact);
break;
case 2 :
printf("Enter the No.: ");
scanf("%d",&N);
if(N==1)
printf("%d is neither Prime nor Composite",N);
else if(N==2)
printf("%d is Prime",N);
else
{
for(i=2;i<=N-1;i++)
{
if(N%i==0)
break;
}
if(i==N)
{
printf("%d is Prime No.",N);
}
else
{
printf("%d is NOT Prime No.",N);
}
}
break;
case 3 :
printf("Enter the No.: ");
scanf("%d",&N);
if(N&0x01==0x01)
printf("%d is ODD No.",N);
else
printf("%d is EVEN No.",N);
break;
case 4 :
exit();
}
}
}
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
// Let Us C (Chapter 4 : Case Control Structure) : Program-2
/* 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>
int _class,N;
void main()
{
clrscr();
printf("\n------------------------------------------------------\n");
printf("1:1st class\t");
printf("2:2nd class\t");
printf("3:3rd class\t");
printf("\nEnter class obtained by student(Enter choice no.): ");
scanf("%d",&_class);
printf("Enter no. of subjects he failed in: ");
scanf("%d",&N);
switch(_class)
{
case 1:
if(N>3)
printf("\nGrace marks not allowed");
else
printf("\n5 Grace marks per subject");
break;
case 2:
if(N>2)
printf("\nGrace marks not allowed");
else
printf("\n4 Grace marks per subject");
break;
case 3:
if(N>1)
printf("\nGrace marks not allowed");
else
printf("\n5 Grace marks per subject");
break;
}
}
Comments
Post a Comment