0% found this document useful (0 votes)
15 views2 pages

18 Lesson 18 Example If Program

The document describes a C program that calculates the average of three test scores entered by the user and assigns a letter grade based on the average. The grading scale is defined, with grades ranging from A to F, and includes checks for invalid input. The program uses conditional statements to output the corresponding letter grade or indicate if the input is not valid.

Uploaded by

sdw7rk7wdv
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

18 Lesson 18 Example If Program

The document describes a C program that calculates the average of three test scores entered by the user and assigns a letter grade based on the average. The grading scale is defined, with grades ranging from A to F, and includes checks for invalid input. The program uses conditional statements to output the corresponding letter grade or indicate if the input is not valid.

Uploaded by

sdw7rk7wdv
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Example program – Letter grade program (Video)

Write a program where user will enter 3 test scores, calculate the average of the 3 test scores and print
the letter grade using the average.

Average Letter grade


90 – 100 A
80 – 89 B
70 – 79 C
0 – 69 F
Otherwise not valid
#include <stdio.h>

int main()
{
//Declaration
int test1, test2, test3, sum;
int avg;

//Data/input
printf("Enter test1 : ");
scanf("%d", &test1);

printf("Enter test2 : ");


scanf("%d", &test2);

printf("Enter test3 : ");


scanf("%d", &test3);

//Calculation
sum = test1 + test2 + test3;
avg = sum / 3;

//if statements and output


if (avg >= 90 && avg <= 100)
printf("A\n");
if (avg >= 80 && avg <= 89)
printf("B\n");
if (avg >= 70 && avg <= 79)
printf("C\n");
if (avg >= 0 && avg <= 69)
printf("F\n");
if (avg < 0)
printf("Not valid, less than 0\n");
if (avg >100)
printf("Not valid, greater 100\n");

return 0;
}

Or

if (avg < 0 || avg > 100)


printf("Not valid\n");

You might also like