New to the boards and to C++

currently working on a problem that I just do not understand.

so far C++ has been smooth, until I hit the loop part. I am just not getting it.

Question:


Please help :)
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
int product = 1;

//for loop structure
//for(initialize ; condition for the loop to execute ; manipulation)
for(int i = 3; i <= 11; i++)    //go from i = 3 to 11 inclusive
{
    product = product * i; //multiply product by i and assign result back to product
    /*
    when i  = 3, product = 1 * 3 = 3,
    when i = 4, product = 3 * 4 = 12
    wehen i = 5, product = 12 * 5 = 60
    and so on...
    */
}
cout<<"The prodcut is "<<product<<endl;

//while loop structure
/*
    intialize
    while(condition for the loop to execute)
    {
     .....
     manipulation   
    }
*/
int product = 1;
int i = 3; //initializer_list
while(i <= 11) //conditional
{
    product = product * i;
    i++; //manipulate
}
cout<<"The product is "<<product<<endl;

//more info: http://www.cplusplus.com/doc/tutorial/control/ 


Hope that helps
closed account (1CfG1hU5)
putting this to form with main.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
//#include <iostream>

int main()
 {
   long product = 1, i;

  //for loop structure
  //for(initialize ; condition for the loop to execute ; manipulation)
  for(i = 3; i <= 11; i++)    //go from i = 3 to 11 inclusive
   {
    product = product * i; //multiply product by i and assign result back to product
    printf("the product is %lu\n", product); // display after each multiply
   /*
    when i  = 3, product = 1 * 3 = 3,
    when i = 4, product = 3 * 4 = 12
    wehen i = 5, product = 12 * 5 = 60
    and so on...
    */
   }

//printf("the product is %d\n\n", product);
//cout << "The prodcut is "<< product;

//while loop structure
/*
    intialize
    while(condition for the loop to execute)
    {
     .....
     manipulation   
    }
*/

product = 1;
i = 3; //initializer_list

while(i <= 11) //conditional
  {
    product = product * i;
    printf("2nd loop product is %lu\n", product);
    i++; //manipulate
  }

//printf("the product is %d\n\n", product);
//cout << "The product is " << product;

return 0;
}


see about making cout work later
Last edited on
closed account (1CfG1hU5)
this worked when I added the line "using namespace std;" the cout functions worked after

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

int main()
 {
   long product = 1;
   int i;
   using namespace std;

  for(i = 3; i <= 11; i++)    //go from i = 3 to 11 inclusive
   {
    product = product * i; //multiply product by i and assign result back to product
    cout << "The prodcut is " << product << "\n";
   }

product = 1; i = 3;

while(i <= 11)
  {
    product = product * i;
    cout << "2nd loop product is " << product << "\n";
    i++;
  }

return 0;

}
Last edited on
Topic archived. No new replies allowed.