Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   find out the average of odd numbers -do-while statement. (http://www.programmingforums.org/showthread.php?t=15746)

LAYAN-2008 May 2nd, 2008 2:23 AM

find out the average of odd numbers -do-while statement.
 
hi , I want to know if my answer is true??and I want your opinion in my answer..
• Write a program fragment to find out the average of odd numbers between two numbers given by the user using a do-while statement?


int sum=0;
int X=50;
int Y = 70;
int count=0;
do{
X++;
if(X%2!=0)
{
sum +=X;
count++;
}
}
while(X<Y);
int average = sum/count;
System.out.println("average=" + average );

Freaky Chris May 2nd, 2008 2:41 AM

Re: find out the average of odd numbers -do-while statement.
 
Looks like it would give the correct results, without testing i will say that you may have to change < to <= and possible change the point at which you increment X.

P.S Use [code] tags

Chris

JONY SMITH May 2nd, 2008 3:40 AM

Re: find out the average of odd numbers -do-while statement.
 
Quote:

Originally Posted by Freaky Chris (Post 144688)
Looks like it would give the correct results, without testing i will say that you may have to change < to <= and possible change the point at which you increment X.

P.S Use [code] tags

Chris

yes it will give you right answer possibly. Try with <= and note out different hope one will be the right answer.

tomisinbamgbelu May 2nd, 2008 7:45 AM

Re: find out the average of odd numbers -do-while statement.
 
your code is quite right but the question says given by the user. ending with an odd no is not provided for, so the while statement should read x<=y; so as 2 test d last number, in case its odd.

misho May 6th, 2008 3:24 PM

Re: find out the average of odd numbers -do-while statement.
 
The code as you have written it should work; however, that's based on X and Y being even. If X were odd, the "X++;" line would be bad. If Y were odd, the "X<Y" condition would be bad.
For arbitrary X and Y, you'd either need to modify the rest of the code, or add something like the following near the beginning:

:

  1. if(X%2 != 0) X--;
  2. if(Y%2 != 0) Y++;


I'd also add that since consecutive odd numbers are 2 apart, incrementing by 1 then checking if your number is odd or even is kind of wasteful. Something like the following would be better:

:

  1. //assuming X and Y have been declared and are even by this point
  2. //and that Y>X
  3. int count = 0, sum = 0;
  4. X++;
  5. do {
  6. sum += X;
  7. count++;
  8. X += 2; //or X++;X++; , not really sure which is faster
  9. } while (X < Y);
  10. int average = sum / count;


I was going to point out that average may not be an int, but you can prove that the average is (X+Y)/2 (again, X and Y are both even, or both odd, I guess), so it has to be.


All times are GMT -5. The time now is 2:14 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC