• Post Reply Bookmark Topic Watch Topic
  • New Topic

A "Multiples" program  RSS feed

 
Mason BarnHart
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So in my online Java class Im almost completely lost on this program. java as a whole is pretty much killing me, with my only prior experience to programming being a basic intro to C++.

Write a program called Multiples that prompts the user for: a whole number n; a starting number(inclusive);an ending number (inclusive). The program should output all numbers between the starting and ending number that are a multiple of the number n. Your solution must use a for-loop. Here is an example output from running the program:

Enter the starting number: 10
Enter the ending number: 20
Enter a
whole number N: 3

Multiples of 3 between 10 and 20: 12, 15, 18


Any help would be greatly appreciated!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Start by posting your code, and point out where you are having difficulties. (Be sure to UseCodeTags.)
 
Mason BarnHart
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont really have much so far, but what I have is just the beginning pretty much

 
Mason BarnHart
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The part Im struggling with is just the conceptual part of the for-statement. I understand what it does but I just dont see a good way of how to implement it in this situation and then code it.
 
Sumit Bisht
Ranch Hand
Posts: 331
Python Ruby Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mason,
As you probably know, basic for loop is similar to C++
for(loop variable beginning value; loop continue condition; change in the value per iteration)

You can therefore apply your problem into these places where the change will be 1. This will loop you through all the values- from starting till the end.
Inside the for loop, you can use continue statement to exit the current iteration of the loop and move over to next.
Hint: Try using the % operator to check if the variable is a multiple of a number or not

So, conceptually, your loop will be something like:
for(... ; ... ; ...)
{
if( condition to check if the variable is a multiple)
{
continue;
}
// perform rest of your work here
}
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mason BarnHart wrote:The part Im struggling with is just the conceptual part of the for-statement. I understand what it does but I just dont see a good way of how to implement it in this situation and then code it.

OK, well don't bother with it then - for now, at least.

Imagine that this is a kid's game using wooden blocks with numbers on them. How do they know which number to pick up first, which numbers are valid, and (most important of all) when they've completed the task?

Write it down on paper, and in English (or your native language), and don't write another line of Java code until you understand the problem forwards, backwards and sideways.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!