The case Control Structure | Let us C solution with details description and tutorials | yashwant kanetkar
[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.
The outline of this program is given below:
/* A menu driven program */
main( )
{
int choice ;
while ( 1 )
{
printf ( "\n1. Factorial" ) ;
printf ( "\n2. Prime" ) ;
printf ( "\n3. Odd/Even" ) ;
printf ( "\n4. Exit" ) ;
printf ( "\nYour choice? " ) ;
scanf ( "%d", &choice ) ;
switch ( choice )
{
case 1 :
/* logic for factorial of a number */
break ;
case 2 :
/* logic for deciding prime number */
break ;
case 3 :
/* logic for odd/even */
break ; case 4 :
exit( ) ;
}
Show Solutions
Hide Solutions
/*
[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
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function
{
int choice, factorial, temp, number, i;
while (1)
{
printf("\n 1. for Factorial");
printf("\n 2. for Prime");
printf("\n 3. for Odd/ Even");
printf("\n 4. for Exit");
printf("\n Your Choice? : ");
scanf ("%d", &choice); // input choice
switch (choice)
{
case 1:
// 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:
// detects a prime number.
printf("Enter the Number:");
scanf ("%d", &number);
if (number==1)
{
printf("The Number 1 is a Unique Number. \n ");
}
for (i=2; i<=number-1; i++)
{
if (number%i==0)
{
printf(" Number is not prime.\n");
break;
}
if (number==i || number%i !=0)
{
printf(" Number is Prime.\n");
break;
}
}
break;
case 3:
// detects Even or 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");
}
break;
case 4:
exit(1);
default:
printf(" wrong choice. Please try again. \n"); // in case wrong choice
break;
}
}
getch();
return 0; // return int value
}
[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
Show Solutions
Hide Solutions
/*
[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> //header file
#include <conio.h> //header file
int main() //main function
{
int c,s;
printf("\n Enter the class obtained by the student and the number of subjects he has failed in.");
scanf("%d%d",&c,&s);
switch(c)
{
case 1:
switch(s>3)
{
case 1:
printf("\n No grace.");
break;
case 0:
printf("\n Grace of 5 marks per subject.");
break;
}
break;
case 2:
switch(s>2)
{
case 1:
printf("\n No grace.");
break;
case 0:
printf("\nGrace of 4 marks per subject.");
break;
}
break;
case 3:
switch(s>1)
{
case 1:
printf("\n No grace.");
break;
case 0:
printf("\n Grace of 5 marks per subject.");
break;
}
break;
}
getch();
return 0; // return int value
}
Comments
Post a Comment