# Thread: Fault average

1. ## Fault average

Hello friends,

I am trying to make a simple average function but the resukts are faulty

say i enter 45 ,51 ,23 for 3 numbers but average shows 39.00 instead of 39.66

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

int main(){

average();

return 0;

}

void average(){
int a,b,c;
int total;
float avg = 0;
printf("\nEnter 3 numbers: ");
scanf("\n %d %d %d",&a,&b,&c);

total = a+b+c;
printf("Sum of 3 numbers is %d",total);
avg =  total/3;
printf("\nAverage of 3 numbers is %.2f",avg);
return avg;
}```

2. You should do floating point division, say by involving a type cast, rather than integer division.

3. Declare "total" as a float instead of an int

4. Originally Posted by Click_here
Declare "total" as a float instead of an int
Or:
Code:
`avg = total / 3.0;`
And let type promotion do its part.

5. Originally Posted by flp1969
Or:
Code:
`avg = total / 3.0;`
And let type promotion do its part.
Integer promotion still takes place, just earlier.

6. Originally Posted by Click_here
Integer promotion still takes place, just earlier.
A small correction:
Code:
`avg = total / 3.0f;`
avg is float, total is int and 3.0f is float... 'total' will be promoted to float to satisfy the expression.
There is no need to "retipify" 'total' to float here.

Popular pages Recent additions