programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# How to use a for loop to calculate the Product of consecutive numbers

C C Campbell
Ranch Hand
Posts: 30
I wanted to know if I was off to the right start. I am trying to write a program using the for loop the calculate the product of the consecutive numbers 4 through 8 but so for I am getting 3 values output and I only want 1 value at the print out.

The code I am using outputs the numbers too large. I am trying to see where I went wrong.

Campbell Ritchie
Marshal
Posts: 56570
172
Welcome to the Ranch

Never use the postfix ++ and -- operators to the right of an = assignment operator. You can read more about it here.
Why are you using a loop and having 4 numbers in it? I think you need to reconsider your logic. You can probably do that with a loop and 1 number, but why 4?

I shall try and edit your post and add code tags (always use them: you can see how much better it looks ) and remove some of the unnecessary blank lines.

Campbell Ritchie
Marshal
Posts: 56570
172
How many numbers are you multiplying in line 8?

Carey Brown
Saloon Keeper
Posts: 3328
46
You need an int variable for the result initialized to 1. Then loop and multiply the result by the loop counter. Then, outside of the loop, print the result.

C C Campbell
Ranch Hand
Posts: 30
In line 8 I was trying to use the variables to multiply 4 *5*6*7*8, I know there is a easier way, I am just grasping for straws as I couldn't get the formula right I started out with just the following code but it evolved from there possibly in the wrong direction

for ( int i = 4 ; i <= 8; i++)

{

int j = i++;

System.out.print( + (i*j) );
}

Carey Brown
Saloon Keeper
Posts: 3328
46
1. put your print outside of the loop.
2. you are already incrementing i in the for(...), don't do it inside the loop as well.
3. Where is your 'result' variable?

C C Campbell
Ranch Hand
Posts: 30
Carey Brown wrote:You need an int variable for the result initialized to 1. Then loop and multiply the result by the loop counter. Then, outside of the loop, print the result.

Thank you this sounds right but I am setting up my counter wrong I think and when print outside of the loop the error comes up because initialize the i variable in the for statement. I don't know that for statement may have been the problem to begin with but the assignment calls for a for loop.

Piet Souris
Master Rancher
Posts: 2044
75
hi C.C.,

can you tell us what the assignment was?

Greetz,
Piet

Stefan Evans
Bartender
Posts: 1837
10
Your for loop and counter look fine.
I think the concept you are struggling to understand is the looping itself.
I suggest you go right back to basics:

Should print out for you:
4
5
6
7
8

The line of code with the println statement executes five times. Once for each value of i you have set up.
Given that, how could you calculate 4 x 5 x 6 x 7 x 8 ?

(hint: you people have told you you need a variable to store the result of the calculation in. )

Guillermo Ishi
Ranch Hand
Posts: 789
print product of consecutive numbers would be like 4x5x6x7x8

Carey Brown
Saloon Keeper
Posts: 3328
46
Guillermo Ishi wrote:print product of consecutive numbers would be like 4x5x6x7x8

I can't fault you too much because I've been guilty of this myself. We try not to give the OP the answer but lead them towards it so that they can learn.

Guillermo Ishi
Ranch Hand
Posts: 789
Carey Brown wrote:I can't fault you too much because I've been guilty of this myself. We try not to give the OP the answer but lead them towards it so that they can learn.

I know, but he's just confused. Learning by seeing good code is effective too.

Junilu Lacar
Sheriff
Posts: 11494
180
Guillermo Ishi wrote:I know, but he's just confused. Learning by seeing good code is effective too.

Sure, but if we ever do that (and I've done it a few times), we let the OP come up with their own solution first. If you just give them your solution without giving them a chance to do it themselves, you rob them of a learning opportunity.

Guillermo Ishi
Ranch Hand
Posts: 789

Stefan Evans
Bartender
Posts: 1837
10
lol. Something is wrong on the internet

I must admit I was sorely tempted to write EXACTLY that answer. Except I think I would have written out the multiplication fully as subtotal = subtotal * i, rather than using the *= syntax.
It's not incorrect, just a little harder to read for the beginner :-)

Potentially also putting a System.out.println displaying the current values of i and subtotal inside the for loop can aid understanding of what is happening.

C C Campbell
Ranch Hand
Posts: 30
Stefan Evans wrote:lol. Something is wrong on the internet

I must admit I was sorely tempted to write EXACTLY that answer. Except I think I would have written out the multiplication fully as subtotal = subtotal * i, rather than using the *= syntax.
It's not incorrect, just a little harder to read for the beginner :-)

Potentially also putting a System.out.println displaying the current values of i and subtotal inside the for loop can aid understanding of what is happening.

LOL thanks everyone I have to laugh at myself now that I see how simple it was.

Liutauras Vilda
Sheriff
Posts: 4923
334
C C Campbell wrote:LOL thanks everyone I have to laugh at myself now that I see how simple it was.

Yes, Java is a very simple programming language, but quite often you have to be a genius to understand the simplicity.
Rephrased Dennis Ritchie original thought: "UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity."

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?