1. ## while loop

I am trying to design a program that ask the user to enter 5 integer, then the program will calculate the average

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

int main()
{
int num;
int counter, total, average;

total=0;
counter =0;
while ( counter<5 ) {
printf("Enter number:");
scanf("%d" , &num );
total += num;
}

average= total/5;

printf("average is %d" , average);

return 0;
}```
The error I got it is an infinite loop .
what should i do about it ?

thank you 2. Within the body of the while loop, you might want to change the value of counter. Otherwise it will always have a value of zero, which is always less than five. 3. @loongkee your counter is not incrementing hence the value of counter will always remain zero and hence infinite loop

try the below.
Code:
```#include<stdio.h>

int main()
{
int num;
int counter, total, average;

total=0;
counter =0;
while ( counter<5 ) {
printf("Enter number:");
scanf("%d" , &num );
total += num;
counter++;
}

average= total/5;

printf("average is %d" , average);

return 0;
}``` 4. Originally Posted by grumpy Within the body of the while loop, you might want to change the value of counter. Otherwise it will always have a value of zero, which is always less than five.
yes, you forget to set counter. when you enter one number, it must beincreased. 5. alternatively
Code:
```#include<stdio.h>

int main(void){
int num;
int counter, total;

total = counter = 0;

while (counter++ < 5) {
printf("%d: Enter number:",counter);
scanf("%d" , &num );
total += num;
}

printf("average is %d" , (total/5) );

return 0;
}```
I cleaned it up a bit but the idea is that you can use the ++ operator to incrment your counter variable without a separate
Code:
`counter++`
statement 6. ok ..got it ..
Thank you very much !!

I have another problem

I put float , but the answer give integer . May I know what's wrong ?

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

float fnCalcAvg( int , int );
void fnPrint(float);

int main()
{
int a,b;
float avg;

avg=fnCalcAvg(a , b);

fnPrint(avg);

return 0;
}

{
printf("Enter 2 value:");
scanf("%d%d", x, y );
}

float fnCalcAvg( int x , int y )
{
float average;
average= (x+y)/2;
return(average);
}

void fnPrint( float avg1)
{
printf("The average is %.2f", avg1);
}``` 7. Code:
```float fnCalcAvg( int x , int y )
{
float average;
average= (x+y)/2;
return(average);
}```
x and y are ints, when you devide int by an int, you get an int, THEN it assigns it to average, a float (double)

you either need to cast an int to a float (double) for the function, or change the ints to floats (double)

normally this is one of the first things taught in classes. 8. Originally Posted by loongkee Code:
```float fnCalcAvg( int x , int y )
{
float average;
average= (x+y)/2;
return(average);
}```
x+y is computed using integer arithmetic, and then divided by 2, again using integer arithmetic (which will truncate, and any fractional part is lost). Only then, is the resultant integer converted to float, in order to store the value in average. The fact that average is of type float does not make the addition or the division occur using floating point.

If you want the calculation done using floating point, you have to force conversion to float before doing the division. Options might include
Code:
`    average = (x + y)/2.0f;      /*   2.0f is of type float, so (x + y) is computed as an integer and then converted to float, before dividing by 2.0f.   */`
or
Code:
`    average = (x + y)/((float)2);    /*   converts int 2 to float explicitly.  Since now dividing by float, the value of (x + y) is converted to float, before doing the division */`
or
Code:
`    average = ((float)x + y)/2;      /*  x is converted to float explicitly.   Because y is being added to a float, y is converted to float before doing the addition.   Then (since ((float)x + y) is of type float, 2 is also converted to float, before doing the addition */`
There are many other possibilities. 9. ok..thank you

I am very weak in C .. 10. Originally Posted by loongkee ok..thank you

I am very weak in C ..
We all were at one point and if you're paying attention you realize you learn something new everyday. C is a succint language but has many subtleties that can lead to unintended consequences. The more you use it the more you learn. Keep it up. Popular pages Recent additions 