Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 2nd, 2008, 3:27 AM   #1
LAYAN-2008
Newbie
 
Join Date: Apr 2008
Posts: 3
Rep Power: 0 LAYAN-2008 is on a distinguished road
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.

java Syntax (Toggle Plain Text)
  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..

Last edited by big_k105; May 2nd, 2008 at 11:05 AM. Reason: Added Code Tags
LAYAN-2008 is offline   Reply With Quote
Old May 2nd, 2008, 10:52 AM   #2
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
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.
OpenLoop is offline   Reply With Quote
Old May 2nd, 2008, 2:42 PM   #3
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 909
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.)
titaniumdecoy is offline   Reply With Quote
Old May 2nd, 2008, 2:47 PM   #4
Freaky Chris
Professional Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 384
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
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.
__________________
Steven Skiena - Algorithms

,[->+>+<<]>>[-<<+>>]>++++++++[-<++++++++>]<+[-<->]>+<<[[-]+++++++++++++++.[-]>]>>[+++++++++.[-]],
brainf**k -- It's such a pretty language
Freaky Chris is offline   Reply With Quote
Old May 12th, 2008, 1:06 AM   #5
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 311
Rep Power: 3 Fall Back Son is on a distinguished road
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.
Fall Back Son is offline   Reply With Quote
Old May 12th, 2008, 2:11 PM   #6
JAlexBrown
Newbie
 
Join Date: May 2008
Posts: 11
Rep Power: 0 JAlexBrown is on a distinguished road
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?
JAlexBrown is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
code to add numbers in an array truBlu1 Java 9 Oct 12th, 2007 11:08 PM
Post count adjustments big_k105 Community Announcements and Feedback 26 Nov 28th, 2005 6:02 PM
User Input for Number Format ericelysia1 Java 0 Jul 21st, 2005 4:41 PM
Loading and Destroying web user controls into a panel see07 C# 0 Feb 2nd, 2005 1:38 PM
Show web user control hidden see07 C# 1 Feb 2nd, 2005 11:35 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:56 AM.

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