In this tutorial of C programs, our task is to write a c program that checks if student result as pass or fail.
To understand this program you must have idea about: if-else decision and for loop making statement
if else:
if statement we found that code inside if block is executed only when condition is found to be true. But what to do in case condition is false.
This is where if-else statement comes handy.
First see the syntax given below,
1 2 3 4 5 6 7 | if(testCondition){ //code to execute for true condtion }else{ //code to execute for false condition } |
for loop:
‘For loop’ is helpful when we need to execute a statement or a block of statement repeatedly and number of iteration required is known.
Syntax:
1 2 3 4 5 | for (initialization; test-condition; increment or decrement){ //code to be iterated } |
Problem: Write a program in C that asks from N Students to enter 3 marks to calculate and print his/her grade based on the average marks.
The answer to this question is given below:
Solution: C program to calculate grades of 5 students from 3 tests using arrays
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | #include <stdio.h> int main(void) { int n = 5; // n students int m = 3; // tests /* 2D array declaration*/ int grade[n][m]; /*fill array*/ int i, j; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { printf("Student: %d Test:%d ", i + 1, j + 1); scanf("%d", &grade[i][j]); } } // Displaying array elements printf("Two Dimensional array elements:\n"); for (i = 0; i < n; i++) { int sum = 0; for (j = 0; j < m; j++) { sum += grade[i][j]; } float avg =(float) sum / 3; printf("\nStudent :%d Avarage : %f Grade =", i + 1, avg); if (avg >= 91 && avg <= 100) printf("A1"); else if (avg >= 81 && avg < 91) printf("A2"); else if (avg >= 71 && avg < 81) printf("B1"); else if (avg >= 61 && avg < 71) printf("B2"); else if (avg >= 51 && avg < 61) printf("C1"); else if (avg >= 41 && avg < 51) printf("C2"); else if (avg >= 33 && avg < 41) printf("D"); else if (avg >= 21 && avg < 33) printf("E1"); else if (avg >= 0 && avg < 21) printf("E2"); else printf("Invalid!"); //www.code4example.com } return 0; } |
You may also like: C Code Examples
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | Student: 1 Test:1 85 Student: 1 Test:2 78 Student: 1 Test:3 96 Student: 2 Test:1 20 Student: 2 Test:2 15 Student: 2 Test:3 42 Student: 3 Test:1 35 Student: 3 Test:2 65 Student: 3 Test:3 20 Student: 4 Test:1 45 Student: 4 Test:2 69 Student: 4 Test:3 45 Student: 5 Test:1 75 Student: 5 Test:2 68 Student: 5 Test:3 21 Two Dimensional array elements: Student :1 Avarage : 86.333336 Grade =A2 Student :2 Avarage : 25.666666 Grade =E1 Student :3 Avarage : 40.000000 Grade =D Student :4 Avarage : 53.000000 Grade =C1 Student :5 Avarage : 54.666668 Grade =C1 |
C program to find grade of a student using for loop,
C program to find grade of a student using if else,
C program to find grade of a student.