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()
{
int grade, num, i;
float sum=0,avg;
char letter[2];
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: ");
scanf ("%i", &grade);
printf ("Now enter the %i grades to be averaged.\n", grade);
if(num<2)
printf("\nEnter atleast 2 grades Enter again: ");
}while(num<2);
for(i=0;i<=num;i++)
{
do{
printf("\nEnter grade #%i:",i+1);
scanf("%d",&grade);
if(grade <0 || grade >100)
{
printf("\n*** Invalid entry. grade must be 0 to 100. ***:");
i--;
}
}while(grade <0 && grade >100);
}
for(i=0;i<=num;i++)
sum=sum+grade;
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;
}