![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Apr 2008
Posts: 3
Rep Power: 0
![]() |
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 ); |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
|
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
__________________
Who said i couldn't program sarcasm = raw_input('Type in a sarcastic remark: ')
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: May 2008
Posts: 2
Rep Power: 0
![]() |
Re: find out the average of odd numbers -do-while statement.
yes it will give you right answer possibly. Try with <= and note out different hope one will be the right answer.
|
|
|
|
|
|
#4 |
|
Programmer
|
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.
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Apr 2008
Posts: 10
Rep Power: 0
![]() |
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: c++ Syntax (Toggle Plain Text)
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: c++ Syntax (Toggle Plain Text)
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| code to add numbers in an array | truBlu1 | Java | 9 | Oct 12th, 2007 10:08 PM |
| random Numbers in openGL | csrocker101 | C++ | 5 | Apr 24th, 2007 8:02 PM |
| Vector problem - find max, min and positions | codylee270 | C++ | 12 | Mar 20th, 2006 12:57 AM |
| Random Number & Average Problem | Hadrurus | Java | 6 | Aug 15th, 2005 1:08 PM |
| Program that can find a patern | Anyways | C++ | 2 | Mar 22nd, 2005 2:27 AM |