# Thread: Calculate and print the average of several grades entered by the user.

1. ## Calculate and print the average of several grades entered by the user.

I am trying to write a C program that calculates and prints the average of several grades entered by the user.The program should first prompt the user to enter the number of grades to process (at least 2grades must be entered by user!) .It should then prompt the user for each grade. As each grade is entered, the program should ensurethat the grades entered are in the grade range of zero to 100. If the grade is not in that range, anerror message should be displayed (as shown below), and the user should be re-prompted for agrade. Any bad grades entered should not be included in the average calculation.Once all values are entered, the program should calculate and print the average of all of the positivevalues entered, rounded to the nearest whole number.At the end display a message for the appropriate letter grade as shown here:90 - 100: A80 - 89: B70 - 79: C60 - 69: D 0 - 59: F

Here is my code but the problem is that it keeps asking for more than 4 grades, I dont know how to make it stop at 4.

Code:
```#include <stdio.h>

int main()
{

float sum=0,avg;
char letter;
do{
printf ("This program caluculates the average of as many grades you wish to enter.\n\n");
printf("\nFirst, enter the number of grades to process: ");

if(num<2)
printf("\nEnter atleast 2 grades Enter again: ");
}while(num<2);

for(i=0;i<=num;i++)
{
do{
{
printf("\n*** Invalid entry. grade must be 0 to 100. ***:");
i--;
}

}
for(i=0;i<=num;i++)
avg=sum/num;
printf("\nThe average of the %d letter entered is %.2f",num,avg);

if(avg>=90 && avg<=100)
printf(letter,"A");
else if(avg >=80 && avg<=89)
printf(letter,"B");
else if(avg >=70 && avg<=79)
printf(letter,"C");
else if(avg >=60 && avg<=69)
printf(letter,"D");
else if(avg >=0 && avg<=59)
printf(letter,"F");
printf("\nYou have a letter grade of %s\n",letter);
return 0;
}``` 2. Code:
```do{
printf ("This program caluculates the average of as many grades you wish to enter.\n\n");
printf("\nFirst, enter the number of grades to process: ");

if(num<2)
printf("\nEnter atleast 2 grades Enter again: ");
}while(num<2);```
In your if and while statements - what value does the num variable have? 3. I am almost done with my code, I need only a selection structure (if, or if else) statement to check for baddata values.
this is my code so far:
Code:
```#include <stdio.h>int main(){
int n, i;
float num, sum=0.0, average;
printf ("This program caluculates the average of as many grades you wish to enter.\n");
printf("\nFirst, enter the number of grades to process: ");
scanf("%d",&n);
printf ("\nNow enter the %d grades to be averaged.\n\n", n);
for(i=0; i<n; ++i)
{
scanf("%f",&num[i]);
sum+=num[i];
}

average=sum/n;
printf("\nThe average of the 4 grades entered is %.2f\n\n",average);

if(average>=90 && average<=100)
printf("\nYou have a letter grade of A ");
else if(average >=80 && average<=89)
printf("\nYou have a letter grade of B ");
else if(average >=70 && average<=79)
printf("\nYou have a letter grade of C ");
else if(average >=60 && average<=69)
printf("\nYou have a letter grade of D ");
else if(average >=0 && average<=59)
printf("\nYou have a letter grade of F ");

getch ();
}``` Popular pages Recent additions entered, grade, grades, program, user 