# Thread: finding average of even numbers at first occurence of odd input

1. ## finding average of even numbers at first occurence of odd input

I am trying to write a c program to read n numbers and reading of numbers should stop at the first occurence of odd numbers and find average of even numbers.
sample input:- 2,4,6,12,18,7
sample output:- 42

what i wrote is

Code:

Code:
```#include<stdio.h>
int main()
{
int i,num,sum=0,cou;
float avg;
while(i>0)
{
printf("enter the integers");
scanf("%d",&num);
if(num%2!=0)
break;
else
printf("%d",num);
}
sum=sum+num;
cou++;
avg=sum/cou;
printf("average of given numbers is %f",avg);
return 0;
}```
Can you please suggest what went wrong in the code ?

2. Lines 15 and 16 should be inside your loop.

> avg=sum/cou;
Beware of integer division.

- make some other variables floats to begin with.
- cast one of them to a float before dividing.

3. Try this

Code:
```#include <stdio.h>
int i=1, cou;
int num,sum=0;
int avg;

int main()
{
do
{
printf("enter the integers: ");
scanf("%d",&num);
if(num%2!=0){
break;
}else{
sum+=num;
}
}while(i);

avg=sum/2;
printf("average of given numbers is %d",avg);
return 0;
}```

4. ## This is not working for the output i want

Originally Posted by Nikosant03
Try this

Code:
```#include <stdio.h>
int i=1, cou;
int num,sum=0;
int avg;

int main()
{
do
{
printf("enter the integers: ");
scanf("%d",&num);
if(num%2!=0){
break;
}else{
sum+=num;
}
}while(i);

avg=sum/2;
printf("average of given numbers is %d",avg);
return 0;
}```
this is not working !!!!

5. You,re sure?

It seems to work.. GDB online Debugger | Code, Compile, Run, Debug online C, C++

6. Well sum/2 isn't going to give you an average, unless you only type in two numbers.

7. oops sorry I ignored the "average"

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

int i=1, cou;
int num,sum=0;
float avg;

int main()
{
do
{
printf("enter the integers: ");
scanf("%d",&num);
if(num%2!=0){
break;
}else{
cou++;
sum+=num;
}
}while(i);

avg=(float)sum/cou;
printf("average of given numbers is %.2f",avg);
return 0;
}```

8. thats the proper question

9. I think the question has an error: it requests for the "average of even numbers", but provides sample input that results in sample output that is the sum of even numbers.

10. ## Thanks !!!! this is correct the given question's samples were incorrect !!!

Originally Posted by nikosant03
oops sorry i ignored the "average"

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

int i=1, cou;
int num,sum=0;
float avg;

int main()
{
do
{
printf("enter the integers: ");
scanf("%d",&num);
if(num%2!=0){
break;
}else{
cou++;
sum+=num;
}
}while(i);

avg=(float)sum/cou;
printf("average of given numbers is %.2f",avg);
return 0;
}```

you are genius !!

11. Thank You for the assertion you were correct the sample input and output given there were wrong and misleading !!

12. Originally Posted by Punju
Thank You for the assertion you were correct the sample input and output given there were wrong and misleading !!
Ah, right.

I'm afraid that nikosant03's example is not very good though:
• It makes use of global variables without good reason. (Well, this program is sufficiently trivial that the use of global variables doesn't matter, but if you aren't skilled enough to correctly write this program on your own with good style, then you should not be declaring your own global variables.)
• It does not check the return value of scanf. Granted, it might be reasonable to assume valid input here, but it is still good practice to check.
• It makes use of a do while loop in an awkward construction, i.e., using the i variable that is effectively a constant when it is unnecessary to begin with. A more conventional way to write that loop would be to use a while (1) or for ( ;; ) construct, relying on the break statement within the loop body to terminate the loop.
• It does not account for the possibility that there are no even numbers entered.
• The indentation needs work.

Looking at the sample input, even though the corresponding sample output is wrong, I might suggest something like this:
Code:
```#include <stdio.h>

int main(void)
{
int count = 0;
int sum = 0;
int num;
printf("Enter even numbers followed by an odd number: ");
while (scanf("%d", &num) == 1 && num % 2 == 0)
{
sum += num;
++count;
}

if (count > 0)
{
printf("Average of the even numbers = %.2f\n", (double)sum / count);
}
else
{
puts("No even numbers were input.");
}

return 0;
}```
The idea is that the user can enter the numbers all on the same line, so you print the prompt once then keep looping while the expected integer input is valid and even. So this makes a reasonable assumption about how to handle invalid input.

13. That's the difference between a pro and a beginner . Room for improvement!!!