Add decimals to average

Hello everyone, first of all i want to apologize for my poor english as it's not my first language
i want the average to show decimals but i don't know how
i know i'm missing something but i don't know what it is... any help would be appreciated

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<stdio.h>
main()
{
	int n=0,i=0,sum=0,n_grd=1,grd=0,avr=0;
	printf("How many grades do you wish to evaluate? ");
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		printf("Type grade number %d: ",n_grd);
		scanf("%d",&grd);
		sum+=grd;
		n_grd++;
	}
	avr=sum/n;
	printf("The average is: %d",avr);
	return(0);
}
Last edited on
The int type stores whole numbers only, make your avr variable a double instead.
Last edited on
I did but it seem like my average becomes 0 regardless of the numbers the program reads
If your sum and n variables are both ints, then the result of dividing them will also be an int.

To fix this, cast one (or both) of them to a floating-point value instead:
avr = (double)sum / n;
Last edited on
Topic archived. No new replies allowed.