Let Us C / Chapter 4 (The Case Control Structure)

                               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;

    }
_____________________________________________________________________

3 comments:

  1. Can you provide solution of Exercise [D] by using nested Switch-case?

    ReplyDelete
  2. #include
    int main()
    {
    int sub,class;

    printf("Enter the Class and Number of Subjects he or she failed");
    scanf("%d\n%d",&class,&sub);


    switch(class){

    case 1:
    switch(sub){
    case 1:
    case 2:
    case 3:
    printf("Grace marks is five per subject");
    exit(1);
    default:
    printf("Sorry, You do not get any grace marks");
    exit(1);
    }
    case 2:
    switch(sub){
    case 1:
    case 2:
    printf("Grace marks is five per subject");
    exit(1);
    default:
    printf("Sorry, You do not get any grace marks");
    exit(1);
    }
    case 3:
    switch(sub){
    case 1:
    printf("Grace marks is five per subject");
    exit(1);
    default:
    printf("Sorry, You do not get any grace marks");
    exit(1);
    }
    }
    return 0;

    }

    ReplyDelete
  3. Blogs on technology helps you stay updated in terms of the latest technology news, launch of gadgets, and many more. Subscribe to technology blogs and use information for your personal or professional use. Programming Help

    ReplyDelete

Let Us C / Chapter 7 (The C Pre-processor)

                               Exercise [C] (a) Write down macro definitions for the following: 1. To test whether a character ente...