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;

    }

Comments

Popular posts from this blog

Chapter 5 : Functions & Pointers (Let us C)

According to a study, the approximate level of intelligence of a person can be calculated using the following formula: i = 2 + ( y + 0.5 x ) Write a program, which will produce a table of values of i, y and x, where y varies from 1 to 6, and, for each value of y, x varies from 5.5 to 12.5 in steps of 0.5.