Reputation: 31
#include <stdio.h>
int main()
{
int n, i;
double num[100], sum = 0.0, average;
printf("Enter the numbers of elements: ");
scanf("%d", &n);
for(i = 0; i < n; i++)
{
printf("Enter number %d: ", i+1);
scanf("%f", &num[i]);
sum += num[i];
}
average = sum / n;
printf("Average = %f", average);
return 0;
}
Hi, I have a problem with displaying the average, displaying numbers from space, how to fix it?
Upvotes: 0
Views: 49
Reputation: 158
Using the correct format specifier for double as stated by @coderredoc resolves it. Use %lf instead of %f.
Upvotes: 0
Reputation: 30926
Correct format specifier for double would be (you will have to use this)
scanf("%lf", &num[i]);
For printf
both %f
or %lf
will do. Note that when using scanf
check the return value of scanf
- in case of failure you will take necessary action. (Wrong input given or some other error).
Upvotes: 1