Program 07: Student subject pass/fail check.
Problem Statement:: Develop a C program that takes marks for three subjects as input. Use a function to check if the student has passed (minimum 40 marks in each subject). Display the average and whether the student passed or failed.
Problem Description:
Input: Three integers representing marks in three subjects.
Output: The average marks and a "Passed" or "Failed" status message.
Constraints: Marks should be in the range 0 to 100; passing requires at least 40 marks in every subject.
Method: Implement a custom function isPassed() to handle the validation logic.
Pgm Logic:
Start.
Input marks for three subjects.
Calculate the average: average = (m1 + m2 + m3) / 3.0.
Call the function isPassed(m1, m2, m3).
Inside the function:
If all three marks are $\ge$ 40, return 1 (Pass).
Else, return 0 (Fail).
In the main program, display the average.
If the function returned 1, print "Passed", otherwise print "Failed".
Stop.
Program Code:
// Purpose: To check subject pass status and calculate average marks using a custom function.
#include <stdio.h>
int isPassed(int m1, int m2, int m3)
{
if (m1 >= 40 && m2 >= 40 && m3 >= 40)
return 1;
else
return 0;
}
void main()
{
int s1, s2, s3;
float average;
printf("Enter marks for three subjects: ");
scanf("%d %d %d", &s1, &s2, &s3);
average = (s1 + s2 + s3) / 3.0;
printf("Average Marks: %.2f\n", average);
if (isPassed(s1, s2, s3))
printf("Result: Passed\n");
else
printf("Result: Failed\n");
}
Output:
Enter marks for three subjects: 50 60 70
Average Marks: 60.00 Result: Passed
RESULT: Thus the program has been executed and the output was verified.
Remarks: Compiled and run in Code::Blocks. This program demonstrates modular programming by separating the logic for "passing" into its own reusable function.
Program Explanation: The main function handles data entry and math, while the isPassed function acts as a "gatekeeper" that checks the specific criteria for passing. Even if the average is high, the student fails if any single subject is below 40.
For all 2026 published C Lab Program posts Index page: click here:
For all 2026 published articles list:click here:
…till the next post, bye-bye & take care
No comments:
Post a Comment