# C PROGRAMS
Function
➤ Introduction to Function
➤ User-defined Functions
➤ Recursion in C
➤ Storage Classes in C
➤ Scope of Variables
Function Programs in C
➤ Functions Examples in C
➤ Add 2 Numbers Function
➤ Find Sum of N numbers
➤ Largest of Three Numbers
➤ Add Subtract Multiply Divide
➤ Calculator Program Functions
➤ Factorial Program Function
➤ GCD of Two Numbers
➤ Find Power of a Number
➤ Find Reverse a Number
➤ Fibonacci Series Function
➤ Prime Number Function
➤ Palindrome Number in C
➤ Armstrong Number in C
Recursion Programs in C
➤ Recursion Examples in C
➤ Fibonacci Series
➤ Find Factorial
➤ Find GCD or HCF
➤ LCM Using Recursion
➤ Tricky C Programs
Write a C Program to Find Grade of a Student using Function. To solve this problem we can use switch case or if-else statements. The below table shows the grading system.
Score in subject | Grade |
>=90 | A |
80-89 | B |
70-79 | C |
60-69 | D |
50-59 | E |
<50 | F |
Prerequisites for finding grade of a student using switch case statements:-
- Switch case in C programming
- Programs on switch case in C
- Introduction to function
- User-defined functions in C
- C Program Using Functions Example
A function is a block of code that performs a specific task. For example, the main is function and every program execution starts from the main function in C programming.
C program is made of one or more functions. Every program must have at least one function with the name main. The execution of the program always starts from the main function and ends with the main function. The main function can call other functions to do some special task.
Find Grade of a Student using Function and If-Else in C
#include<stdio.h>
// function to find grade using if-else
char findGrade(int score)
{
// check score is valid or not
// score is valid if it belongs to 0-100
if(score<0 || score>100) {
return '\0';
}
// find grade for given score
// for score >= 90
if(score>=90 && score<=100)
return 'A';
// for score>=80 and <90
else if(score>=80)
return 'B';
// for score>=70 and <80
else if(score>=70)
return 'C';
// for score>=60 and <70
else if(score>=60)
return 'D';
// for score>=50 and <60
else if(score>=50)
return 'E';
// for score<50
else
return 'F';
}
// main function
int main()
{
// variables
int score;
char grade;
// take score
printf("Enter score(0-100): ");
scanf("%d",&score);
// find grade
grade = findGrade(score);
// display grade
if(grade=='\0')
printf("Invalid Score");
else
printf("Grade: %c\n", grade);
return 0;
}
Output:-
Enter score(0-100): 50
Grade: E
Enter score(0-100): 90
Grade: A
Enter score(0-100): 150
Invalid Score
Enter score(0-100): -30
Invalid Score
Enter score(0-100): 20
Grade: F
In the function first we checked the entered score is valid or not. If the score doesn’t belong to 0 to 100 (inclusive) then it will be treated as invalid score. In that case returns ‘\0’.
If the score is valid then we will start finding the grade based on the given score. From the table, if the score is greater than or equal to 90 then the grade will ‘A’, and so on. For a score lesser than 50 the grade will be ‘F’.
Using Function and Switch Case in C
#include<stdio.h>
// function to find grade using switch-case
char findGrade(int score)
{
// check score is valid or not
// score is valid if it belongs to 0-100
if(score<0 || score>100) {
return '\0';
}
// find grade for given score
switch( score / 10 )
{
case 10:
case 9:
return 'A';
case 8:
return 'B';
case 7:
return 'C';
case 6:
return 'D';
case 5:
return 'E';
default:
return 'F';
}
}
// main function
int main()
{
// variables
int score;
char grade;
// take score
printf("Enter score(0-100): ");
scanf("%d",&score);
// find grade
grade = findGrade(score);
// display grade
if(grade=='\0')
printf("Invalid Score");
else
printf("Grade: %c\n", grade);
return 0;
}
The condition of the switch case is score/10, so, the score every is divided by 10 and the matched label will be executed. For score 90-100 is A, so for labels 9 and 10, the grade will be A. Similarly, for other ranges. If the score is below 50 then score/10 gives below 5 and it doesn’t match with any condition hence, the default statement will be executed.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!
grade = findGrade(score);
How is the code working with this line, the findGrade() is undefined, please explain more.
In the C program, all functions used inside the main() function should be defined or declared before the definition of the main() function. If you define/develop findGrade() after main() function without declaring at the top then the compiler will give an undefined error because it doesn’t know that findGrade() function is defined below. The compiler compiles code from top to bottom.
To resolve this compile time error, the findGrade() function should be defined before the main() function or at least declared before the main() function so that compiler will know there is a findGrade() function defined later.
In the program given in the post, we didn’t declare the function but defined the function before the main() function. Learn more:- Introduction to function