# How to create this forloop

• (2 Pages)
• 1
• 2

## 15 Replies - 991 Views - Last Post: 09 October 2012 - 05:33 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294754&amp;s=5ea24caf441236102db6ed83d4825d0b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 skyline2162

Reputation: -1
• Posts: 112
• Joined: 17-December 11

# How to create this forloop

Posted 08 October 2012 - 09:54 AM

I am trying to sum up the product of the numbers from the begining to the last number of my set of numbers, such as begining = 1 and end = 5, given a change of = 1 I want to do 1*2*3*4*5, I want to use a forloop and have started off with for (int i = begining; i <= last; i+=change), I know that i need to do i*i+delta+i*delta, and I need to do a nested for statement right? But i have no clue how to set up the inner loop

```double total;
for (int i = start; i <= end; i+=delta)
{
}

```

Meant to use this

```double total = 0;
(int i = begining; i <= last; i+=change)

```

Is This A Good Question/Topic? 0

## Replies To: How to create this forloop

### #2 Skydiver

• Code herder

Reputation: 4025
• Posts: 12,821
• Joined: 05-May 12

## Re: How to create this forloop

Posted 08 October 2012 - 10:37 AM

I'm sorry, I don't quite understand your question. You say that you want the sum of the products of numbers, and you gave the example of 1*2*3*4*5. So that is one of the terms of the sum, what are the other terms?

In other words:
```sum =
1 * 2 * 3 * 4 * 5 +
???? +
???? +
:

```

What are the question marks supposed to be filled with?

### #3 skyline2162

Reputation: -1
• Posts: 112
• Joined: 17-December 11

## Re: How to create this forloop

Posted 08 October 2012 - 10:44 AM

So say the user enter the begining number as 1 and the end number as 10, and the change in as +2, so then i would use this loop to output the numbers

```double total = 0;

(int i = begining; i <= last; i+=change)

```

Now this will give the output of 1,3,5,7,9. I want to now multiply 1*3*5*7*9 to give me the result 945. But i need a general loop that will work for any number.

This post has been edited by Skydiver: 08 October 2012 - 10:49 AM
Reason for edit:: Fixed code tags.

• MrCupOfT

Reputation: 2290
• Posts: 9,528
• Joined: 29-May 08

## Re: How to create this forloop

Posted 08 October 2012 - 10:52 AM

Are you serious?
You don't notice what the change is between
1 + 3 + 5 + 7 + 9
and
1 * 3 * 5 * 7 * 9 ?

This post has been edited by AdamSpeight2008: 08 October 2012 - 10:52 AM

### #5 skyline2162

Reputation: -1
• Posts: 112
• Joined: 17-December 11

## Re: How to create this forloop

Posted 08 October 2012 - 10:55 AM

No clue, I am not sure how to keep the running total

### #6 Skydiver

• Code herder

Reputation: 4025
• Posts: 12,821
• Joined: 05-May 12

## Re: How to create this forloop

Posted 08 October 2012 - 10:57 AM

So you are not looking for the sum. You are looking for the product of the sequence of numbers.

The logic is very simple and fits in one line. I think that you are fixated on thinking that you need an inner loop when you actually don't need one. So let me try to approach this obliquely.

Let's for example that you just want to print the numbers and not compute the product, you would go about it using your for loop as:
```for(int i = beginning; i <= end; i += change)
{
Console.WriteLine(i);
}

```

Now, let's say you want to put all those numbers together as a single string. You could do it as:
```string sequence = "";
for(int i = beginning; i <= end; i += change)
{
sequence += " " + i.ToString();
}

```

Now if you wanted to compute the product of those numbers, what would you do within a for loop?

This post has been edited by Skydiver: 08 October 2012 - 10:58 AM

### #7 CharlieMay

• This space intentionally left blank

Reputation: 1719
• Posts: 5,636
• Joined: 25-September 09

## Re: How to create this forloop

Posted 08 October 2012 - 10:59 AM

Since i becomes the value through the iteration of the loop, can't you just create a variable initialized to 1 that holds itself * i until the loop completes, leaving your new variable holding the result?

### #8 Skydiver

• Code herder

Reputation: 4025
• Posts: 12,821
• Joined: 05-May 12

## Re: How to create this forloop

Posted 08 October 2012 - 11:01 AM

Or taken from a different perspective, consider this:
```a = 1;
b = 2;
c = 3;
d = 4;
e = 5;

product = a * b * c * d * e;

```

But this is also the same as:
```step0 = 1 * a;
step1 = step0 * b;
step2 = step1 * c;
step3 = step2 * d;
step4 = step3 * e;
product = step4;

```

By observing this, you can see that CharlieMay's hint is very very good.

This post has been edited by Skydiver: 08 October 2012 - 11:03 AM

### #9 skyline2162

Reputation: -1
• Posts: 112
• Joined: 17-December 11

## Re: How to create this forloop

Posted 08 October 2012 - 11:03 AM

id want to multiply them? within the forloop?

• MrCupOfT

Reputation: 2290
• Posts: 9,528
• Joined: 29-May 08

## Re: How to create this forloop

Posted 08 October 2012 - 11:06 AM

Why not do some investigation experimentation? And find out.

### #11 Skydiver

• Code herder

Reputation: 4025
• Posts: 12,821
• Joined: 05-May 12

## Re: How to create this forloop

Posted 08 October 2012 - 11:07 AM

Yes, just like the way you accumulated the value of sum in your for loop in your old question in the Java Forum: http://www.dreaminco...make-this-loop/ , you would accumulate the product in your for loop now.

### #12 CharlieMay

• This space intentionally left blank

Reputation: 1719
• Posts: 5,636
• Joined: 25-September 09

## Re: How to create this forloop

Posted 08 October 2012 - 11:08 AM

Given that a holds a value and bholds a value how would you write a line of code where c holds the product of those two values?

### #13 skyline2162

Reputation: -1
• Posts: 112
• Joined: 17-December 11

## Re: How to create this forloop

Posted 08 October 2012 - 11:09 AM

would i just do
```double x = 1;
for(int i = beginning; i <= end; i += change)
{
result *= i;
}

```

### #14 CharlieMay

• This space intentionally left blank

Reputation: 1719
• Posts: 5,636
• Joined: 25-September 09

## Re: How to create this forloop

Posted 08 October 2012 - 11:11 AM

Well, close, but then what would x be for? Maybe rename either x to result or result to x but that's the gist of it.

### #15 skyline2162

Reputation: -1
• Posts: 112
• Joined: 17-December 11

## Re: How to create this forloop

Posted 09 October 2012 - 02:46 PM

If i were to change this loop for it to accept a begining numbers greater than a ending number, how would i do that, how can I put an or statement in the for loop? such that my start would be 5 and end would be 1 and change -1, becuase the one I have now would be false in this case, is there a way to do an or statement in a for statement?

what im trying to ask is that I need a loop that will work for both if begining was 5 and end was 1 and if begining was 1 and end was 5?