-1

I'm not totally sure where to start with this program. I was just introduced to loops and have done a couple simple loops. This question doesn't make sense to me. Basically it is supposed to read

answer is 3
answer is 12
answer is 60
answer is 360
answer is 2520
answer is 20160

I can make it read this but only by hard coding... Do I use while statements? if statements? else statements? I am so lost and have been trying for about an hour already.

Paul R
  • 208,748
  • 37
  • 389
  • 560
timwheelz
  • 25
  • 1
  • 4

3 Answers3

4

The problem could be solved with one for loop as follows.

int Product = 1;

for (int i = 3; i <= 8; i++)
{
    Product = Product * i;
    // if desired, do something useful with Product
}
Codor
  • 17,447
  • 9
  • 29
  • 56
  • 1
    This doesn't match the required output. It's actually much easier to do with a single loop multiplying onto some variable – swinefish Feb 22 '16 at 07:47
  • 1
    Yes, I somewhat misunderstood question. The desired outcome in total would be 3 * 4 * 5 * 6 * 7 * 8, not some pairwise products. – Codor Feb 22 '16 at 09:14
  • @Codor Me too..I didn't read the question well. I thought it was to print the pattern infinitely.. My bad. – Nidhin David Feb 22 '16 at 09:17
0

This follows a pattern

3*4 = 12
(3*4)*5 = 12 * 5 = 60
(3*4*5)*6 = 60 * 6 = 360
(3*4*5*6)*7 = 360 * 7 = 2520
(3*4*5*6*7)*8 = 2520 * 8 = 20160 

code in C++

     int result = 1;
     for(int i =3; i<=8;i++)
     {
         result = result * i;
         cout<<"answer is"<<result;
     }
Skywalker
  • 1,590
  • 1
  • 18
  • 36
Nidhin David
  • 2,426
  • 3
  • 31
  • 45
0
int out = 1;
for (int i = 3; i < 9; ++i)
{
    out *= i;
    cout << out << endl;
}
Dave Chandler
  • 651
  • 6
  • 17