Sunday, January 15, 2012

Program with Nested if-else statement

Exercise 15

Program to create report card of a student. The user need to enter the marks of English, Science, Maths, Social Studies of an examination out of 100. The program will find the average marks, if the marks is below 35, the system will prompt “Sorry, the student has failed”. If the marks is more than 20 and less than 35, the system will prompt “The student needs little improvement” and if the marks is below 20, then the system will prompt “The student needs to work hard”. If the marks is more than and equal to 35, the system will prompt “The student has passed”. If the marks is greater than or equal to 35 and less than 60 then the system will prompt “Well Done” and if the marks is greater than or equal to 60, then the system will prompt “Excellent”.

Solution:
/* Program to create Report Card of a student */
#include<stdio.h>
#include<conio.h>


void main()
{
float eng, sc, mth, sst, av;
printf(“Enter the marks of English = ”);
scanf(“%f”, &eng);
printf(“Enter the marks of Science = ”);
scanf(“%f”, &sc);
printf(“Enter the marks of Maths = ”);
scanf(“%f”, &mth);
printf(“Enter the marks of Social Studies = ”);
scanf(“%f”, &sst);
av=(eng+sc+mth+sst)/4;
printf(“Average = %f”, av);
if(av<35)
  {
   printf(“\nSorry, the student has failed”);
   if(av>20)
      printf(“\nThe student needs little improvement”);
   else
      printf(“\nThe student needs to work hard”);
  }
else
  {
    printf(“\nThe student has passed”);
    if(av<60)
      printf(“\nWell Done”);
    else
      printf(“\nExcellent”);
  }
getch();
}
Explanation: In the first part of the program, the system asks the user to enter the marks of individual subjects. Once the marks for each subjects are entered, the average is being calculated and shown on the output window. Now the program counter enters into the decision control structure.
At first the system needs to decide whether the average is below 35 or not. If the first condition is true, then at first the system prompts the “Sorry, the student has failed” and the program counter enters into the nested if-else structure and the program now have to decide whether the average is more than 20 or not. If the average is more than 20, then the system prompts “The student needs little improvement”, otherwise the system prompts “The student needs to word hard” and then the program counter jumps out of the if-else structure and execute the following statements.
If the average is not less than 35 or the average is more than or equal to 35, then the system will prompt “The student has passed” and then the program counter will enter into the else portion. Now again the program counter enters into the nested if-else structure within the else structure. If the average is less than 60, then the system will prompt “Well Done” and if not or the average is more than or equal to 60, then the system will prompt “Excellent”.
The program counter will now reach to the getch() statement or function and wait for the user to press any key of the keyboard to quit the program.
Output of the program is as follows:
Case-I
Enter the marks of English = 30
Enter the marks of Science = 34
Enter the marks of Maths = 37
Enter the marks of Social Studies = 25
Average = 31.500000
Sorry, the student has failed
The student needs little improvement
Case-II
Enter the marks of English = 21
Enter the marks of Science = 16
Enter the marks of Maths = 14
Enter the marks of Social Studies = 20
Average = 17.750000
Sorry, the student has failed
The student needs to work hard
Case-III
Enter the marks of English = 59
Enter the marks of Science = 56
Enter the marks of Maths = 45
Enter the marks of Social Studies = 65
Average = 56.250000
The student has passed
Well Done
Case-IV
Enter the marks of English = 65
Enter the marks of Science = 80
Enter the marks of Maths = 75
Enter the marks of Social Studies = 85
Average = 76.250000
The student has passed
Excellent

No comments:

Post a Comment

Please give your valuable comments or queries if you fell its helpful or if you like the contents of the site. Thanks...