To select a proper grade for the given mark using the if-else-if statement C++ code example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: To select a proper grade for the given mark using the if-else-if statement in C++ programming

To show: How to use the if-else-if statements to select a proper grade for the given mark in C++ programming

 

// printing the students' grades based on their marks

#include <iostream>

using namespace std;

 

int main(void)

{

int mark;

 

cout<<"Enter student's mark: ";

cin>>mark;

if (mark < 40)

cout<<"Student grade = F";

else if (mark < 50)

cout<<"Student grade = E";

else if (mark < 60)

cout<<"Student grade = D";

else if (mark < 70)

cout<<"Student grade = C";

else if (mark < 80)

cout<<"Student grade = B";

else

cout<<"Student grade = A";

cout<<"\n";

 

return 0;

}

 

Output examples:

 

Enter student's mark: 35

Student grade = F

Press any key to continue . . .

 

Enter student's mark: 45

Student grade = E

Press any key to continue . . .

Enter student's mark: 55

Student grade = D

Press any key to continue . . .

 

Enter student's mark: 67

Student grade = C

Press any key to continue . . .

 

Enter student's mark: 78

Student grade = B

Press any key to continue . . .

 

Enter student's mark: 83

Student grade = A

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index