A division-classification program first calculates or accepts a percentage and then maps that value to ranges such as first, second or third division. Validate the percentage before classifying it.
#include <stdio.h>
int main(void) {
double percentage = 67.5;
if (percentage < 0.0 || percentage > 100.0) {
printf("Invalid percentage.\n");
} else if (percentage >= 60.0) {
printf("First Division\n");
} else if (percentage >= 50.0) {
printf("Second Division\n");
} else if (percentage >= 40.0) {
printf("Third Division\n");
} else {
printf("Fail\n");
}
return 0;
}
#include <stdio.h>
int main(void) {
double percentage;
printf("Enter percentage: ");
if (scanf("%lf", &percentage) != 1) {
printf("Invalid input.\n");
return 1;
}
if (percentage < 0.0 || percentage > 100.0) {
printf("Percentage must be from 0 to 100.\n");
} else if (percentage >= 60.0) {
printf("First Division\n");
} else if (percentage >= 50.0) {
printf("Second Division\n");
} else if (percentage >= 40.0) {
printf("Third Division\n");
} else {
printf("Fail\n");
}
return 0;
}
#include <stdio.h>
int main(void) {
double marks[] = {62, 71, 58, 69, 65};
size_t count = sizeof marks / sizeof marks[0];
double total = 0.0;
for (size_t i = 0; i < count; i++) {
total += marks[i];
}
double percentage = total / count;
printf("Percentage = %.2f%%\n", percentage);
if (percentage >= 60.0) {
printf("First Division\n");
} else if (percentage >= 50.0) {
printf("Second Division\n");
} else if (percentage >= 40.0) {
printf("Third Division\n");
} else {
printf("Fail\n");
}
return 0;
}
#include <stdio.h>
int main(void) {
double percentage = 78.5;
if (percentage >= 75.0) {
printf("Distinction\n");
} else if (percentage >= 60.0) {
printf("First Division\n");
} else if (percentage >= 50.0) {
printf("Second Division\n");
} else if (percentage >= 40.0) {
printf("Third Division\n");
} else {
printf("Fail\n");
}
return 0;
}
#include <stdio.h>
double calculate_percentage(const double marks[], size_t count) {
double total = 0.0;
for (size_t i = 0; i < count; i++) total += marks[i];
return total / count;
}
void print_division(double percentage) {
if (percentage >= 75.0) printf("Distinction\n");
else if (percentage >= 60.0) printf("First Division\n");
else if (percentage >= 50.0) printf("Second Division\n");
else if (percentage >= 40.0) printf("Third Division\n");
else printf("Fail\n");
}
int main(void) {
double marks[] = {72, 81, 64, 77, 69};
size_t count = sizeof marks / sizeof marks[0];
double percentage = calculate_percentage(marks, count);
printf("Percentage: %.2f%%\n", percentage);
print_division(percentage);
return 0;
}
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| sadiya ramesh | 13-07-2014 |
| help me out with this please Write a C program that prompts for students’ final grades in 5 units in a given class (the grades are integer values in the range 0-100). The program then displays the class grade average, the highest grade, and how many students in the class failed the course (a grade less than 40 is a failing grade). If a student fails in more than 2 subjects then a message “DISCONTINUATION” is displayed at the very end. | |