C program to accept marks from user and print grade accordingly (>=75 – Distinction, <75 and >=60 – First, <60 and >=50 – Second, <50 and >=35 – Pass, <35 – Fail) using if..else-if statement and also by using logical operators.

This is one of the most common program you will encounter while learning C programming language. Interestingly, this is also one of the easiest programs if you know how to design logic by using logical operators.

## Tutorial 📺

Watch tutorial and learn how to solve this problem. 😉

## Steps 🪜

### To accept marks from user and print grade

Assumptions: Let’s assume, there are only 4 subjects (name of the subjects are up to you) and maximum marks for each subject is 50.

1. Accept 4 floating point numbers from the user and store the values in 4 variables c, cpp, java and python.
2. First of all, check if values are valid or not, marks should be within the range of 0 and 50. Display proper message if invalid value(s) entered by the user.
3. If all values are valid then first calculate the percentage and store in a variable named per. We will determine and print the grade based on percentage.
4. Now, frame the conditions as per the given grade categories in program definition.
5. Each conditional statement must have a printf statement to display the grade with appropriate message.

## Code 💻

``````#include <stdio.h>
#include <conio.h>

void main()
{
float c, cpp, java, python, per;
clrscr();
printf("Please enter the marks for C: ");
scanf("%f", &c);
printf("Please enter the marks for C++: ");
scanf("%f", &cpp);
printf("Please enter the marks for JAVA: ");
scanf("%f", &java);
printf("Please enter the marks for Python: ");
scanf("%f", &python);
// Let's check if marks are valid or not
if (c<0 || cpp<0 || java<0 || python<0 || c>50 || cpp>50 || java>50 || python>50)
printf("Invalid marks entered.");
else
{
// Assuming that per subject maximum marks are 50.
per = ((c + cpp + java + python) / 200) * 100;
// Let's find out the grade.
if (per >= 75)
printf("DISTINCTION");
else if (per < 75 && per >= 60)
printf("FIRST");
else if (per < 60 && per >= 50)
printf("SECOND");
else if (per < 50 && per >= 35)
printf("PASS");
else
printf("FAIL");
}
getch();
}``````

## Example 😍

Let’s assume, the user enters marks for 4 subjects as follows: 49, -9, 39 and 41.

First of all, it will check if entered marks are valid or not. Since, there is a negative value which does not fall in the range 0 and 50, it will display the following message:

``Invalid marks entered``

Let’s take another example and this time user enters 40, 35, 32 and 28. Each and every value is within the range of 0 and 50, which means all marks are valid. Now the percentage will be calculated (((40+35+32+28)/200)*100) and that is 67.5%.

Now, it will test this value with conditional statements to display the grade. First, it will check if per>=75 or not. Since, 67.5 is lesser than 75, so it will move to the next conditional statement. Again, it will check if per<75 and per>=60, now we know that 67.5 is lesser than 75 and greater than 60.

Finally, it will display the message:

``Grade: FIRST``

This is how we write C program to accept marks from the user and print grade accordingly.