Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   count multiples of 6 between two numbers given by the user. (http://www.programmingforums.org/showthread.php?t=15747)

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

count multiples of 6 between two numbers given by the user.
 
Hi all, Is my answer true .. and I want your opinion in my answer
• Write a program fragment to count multiples of 6 between two numbers given by the user.

:

  1. int X=30;
  2. int Y = 60;
  3. int count=0;
  4. do {
  5.         X++;
  6.         if(X%6==0) 
  7.         {
  8.                 count++;
  9.         }
  10. } while(X<Y);
  11. System.out.println("Count of Multiplies=" + count );


Thank you..

OpenLoop May 2nd, 2008 9:52 AM

Re: count multiples of 6 between two numbers given by the user.
 
I think you got it. Quick note though, the two number (X and Y) should be inputted by the user, not you, so you should probably ask for them.

titaniumdecoy May 2nd, 2008 1:42 PM

Re: count multiples of 6 between two numbers given by the user.
 
Your logic is not quite correct. Try X=35, Y=35 or X=42, Y=48.

Hint: Use a while loop instead of a do/while loop. The latter should be avoided when possible. (There is another problem with your code, but I will let you figure out what it is.)

Freaky Chris May 2nd, 2008 1:47 PM

Re: count multiples of 6 between two numbers given by the user.
 
i pointed this out to him in his other post, as it was a very similar problem and he made the very same logically mistake as he has done here.

Fall Back Son May 12th, 2008 12:06 AM

Re: count multiples of 6 between two numbers given by the user.
 
I have a tip; don't make it so obvious that you are doing homework problems. Goodness.

JAlexBrown May 12th, 2008 1:11 PM

Re: count multiples of 6 between two numbers given by the user.
 
I agree with titaniumdecoy...don't use a do...while loop. Use a while loop (or I suppose a for loop would work in this case as well). Also, have X and Y be input. You could use a scanner or you could use a JOptionPane. Also, why do you increment X before you check to see if it's a multiple of six?


All times are GMT -5. The time now is 1:18 AM.

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