• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
C C Campbell
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many numbers are you multiplying in line 8?
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi C.C.,

can you tell us what the assignment was?

Greetz,
Piet
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
print product of consecutive numbers would be like 4x5x6x7x8


 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I broke your internets.

 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!