How to find average

I have this program that I am supposed to have a program that writes the sum and how many quarters. I am mainly just wondering how to find the average sales per quarter. Code below

#include<stdio.h>
int main()
{ int n,sum=0,sales,i; 2
2
printf("How many years do you wish to average ");
scanf("%d",&n);
if(n==0)
{
printf("Year must be at least one \n and renter ");
scanf("%d",&n);
}
for( i=1;i<=n;i++)
{
printf("year %d\n",i);
for(int j=1;j<=4;j++)
{
printf(" Sales amount of quarter %d\t",j);
scanf("%d",&sales);
if(sales<0)
{
printf("Sales amount should be greater than zero");
}
sum=sum+sales;

}

}
printf("Over a period %d quarters, %d\t",(i-1)*4,sum);


return 0;
}
Add all the quarter's sales together then divide the total by the number of quarters you added together.
average is just sum/quarters
preferably as a double.

double avg = (double)(sum)/quarters;

and %f prints a double value. try "%1.2f" if you don't need high precision.

Are you intentionally writing C instead of C++?




Topic archived. No new replies allowed.