Monday 29 August 2011

You are required to write a program which should take input from user in the form of characters A or B. Based upon user’s input you should calculate no. of A grades. You should use while loop or do/while loop for taking input and if / else condition for making decisions.

Detailed Description:
The program should display like;

Please Enter Grade (‘A’ OR ‘B’ )


Then the program should take 10 inputs one by one,


  • After taking 10 inputs, you should display no. of A grades.

  • If A grades are less than or equal to 2, you should display a message “Your class is Poor!”.

  • If A grades are less than or equal to 7, you should display a message “Your class is Good!”.

  • If A grades are greater than or equal to 8, you should display a message “Your class is Brilliant!”.

  • The user should enter either A or B. If user has entered other than A or B, e.g. C,D,E etc. Your program should display a message like; 


     "Please Enter 'A' or 'B' grade only!"



Sample Input and Output



Please Enter Grade of student 1 :

A

Please Enter Grade of student 2 :

A

Please Enter Grade of student 3 :

B

Please Enter Grade of student 4 :

A

Please Enter Grade of student 5 :

B

Please Enter Grade of student 6 :

B

Please Enter Grade of student 7 :

A

Please Enter Grade of student 8 :

B

Please Enter Grade of student 9 :

C

Please Enter ‘A’ or ‘B’ grade only!

Please Enter Grade of student 9 :

A

Please Enter Grade of student 10 :

A

Total No. of A Grades = 6


Your Class is Good!



Answer

// header files for input and output
#include <iostream.h> 
#include <conio.h>

main(){   // program will start execution from here
// declaring required variables
int  A_Grades = 0,    // will calculate no. of A grades
     B_Grades = 0,   // will calculate no. of grades, other than A
     count = 1;      // will be used as student count
   
     char input; // used to store input by the user

do {
   
    cout << "Please Enter Grade of Student \t" << count << endl;  // taking user input for each student
    cin >> input;  

     if (input == 'A')    // checking if the input is A grade
      {    
        A_Grades++;          // adding 1 to A variable
        count++;        
        }       
   
     else if (input == 'B')   // checking if the input is B grade
       {   
        B_Grades++;   // adding 1 to other variable
        count++;        
        }
   
      else   // If input is not A or B
         {
         cout << "Please Enter 'A' or 'B' grade only!" << endl;
           }   
          
     }while (count <= 10);
   
      
    
       
// displaying no. of A Grades in the class

cout << "\nTotal No. of A Grades \t" << A_Grades << endl; 
    
     if (A_Grades <= 2)    {      // checking if A grades are equal to or more than 8
     cout << "\nYour Class is Poor\t" << endl;  // displaying message accordingly
     }    
     else if (A_Grades <=7 )    {      // checking if A grades are equal to or more than 8
     cout << "\nYour Class is Good\t" << endl;  // displaying message accordingly
     }    
     else if (A_Grades >= 8)    {      // checking if A grades are equal to or more than 8
     cout << "\nYour Class is Brilliant\t" << endl;  // displaying message accordingly
     }    
    
getche(); // press any key to exit the program
}


No comments:

Post a Comment