loop coding problem

/* Question :
* Modify the program to find the sum of the
* square of the numbers from 1 to an upperbound
* ie, 1*1 ,2*2 , 3*3
*
*(I don't actually know how it works , can I have someone to teach me ?)
*(Thanks)
*/

#include <iostream>
using namespace std ;

int main ()
{
int sum = 0 ;
int upperbound ;

cout << "Enter the upperbound \t";
cin >> upperbound ;

int number = 1;
while (number <= upperbound)
{
sum = sum + number ;
number ++;
cout << number << endl;
}

cout << "The loop of upperbound is " << sum << endl;

return 0 ;

}
Can you ask specific questions? What don't you understand about the code?

You can learn a lot about how it works by running it and observing the output when you give different inputs.
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
/*
* Question :
* Modify the program to find the sum of the
* square of the numbers from 1 to an upperbound
* ie. 1*1, 2*2, 3*3
*/

#include <iostream>

using namespace std;

int main ()
{
    int sum = 0;
    int square; // Add this variable
    int upperbound;

    cout << "Enter the upperbound \t";
    cin >> upperbound;

    int number = 1;
    while (number <= upperbound)
    {
        square = number * number; // Adding this line will give you the square
        cout << number << "*" << number << " = " << square << endl; // Modifying this line will display the calculations more clearly
        sum = sum + square;
        number ++;
    }

    cout << "The sum of the squares from 1 to " << number << " is " << sum << endl; // Modified to make it more clear

    return 0;

}
Last edited on
dhayden ^ ,

sorry , I still don't know how the code works , I have try understanding the code flow but I still
don't get it too much . Sorry .

PBachmann^,

Thanks
Last edited on
PBachmann ^

Why I can't do like this ? Isn't is the same ?

/*
*Modify the program to find the sum of all the sqaure
*of all the numbers from 1 to an upperbound . ie:
*1*1 , 2*2 , 3*3 , 4*4 ....
*/

#include<iostream>
using namespace std;

int main ()
{
int sum = 0 ;
int upperbound ;

cout << "Enter the upperbound \t " ;
cin >> upperbound ;

int number = 1;
while (number <= upperbound)
{
sum = sum + number;
number = number * number ; / * <=here */
number ++ ;

cout << number << endl;
}
cout << "The loop of the upperbound is " << sum << endl;

return 0 ;
}
PBachman ^

Because,
sum = 0

number = 1

number = 1*1

//then, do the other numbers as well , so i add ,

number ++

The first time through the loop:
number is 1.
number = number * number sets number to 1
number++ sets number to 2.

Next time through the loop:
number is 2
number = number * number sets number to 4
number ++ sets number to 5

Next time through the loop:
number is 5
number = number * number sets number to 25
number ++ sets number to 26

etc.
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
/*
*Modify the program to find the sum of all the sqaure
*of all the numbers from 1 to an upperbound . ie:
*1*1 , 2*2 , 3*3 , 4*4 ....
*/

#include<iostream>
using namespace std;

int main ()
{
int sum = 0 ;
int upperbound ; 

cout << "Enter the upperbound \t " ;
cin >> upperbound ; 

int number = 1;
while (number <= upperbound)
{
//sum = sum + number;        <= this causes the error 
//number = number * number ; <= that dhayden explained

//vvvvvvvv
sum = sum + number * number; 
//^^^^^^^^This can be used instead of square = number * number;

number ++ ; 

cout << number << endl;
}
cout << "The loop of the upperbound is " << sum << endl;

return 0 ;
}
Isn't ? roughly , i still can get it ...

But how to solve this ? Sorry , I have try my best ... to solve this until like this and get stuck ...

/*
* Modify the program to compute the product of the numbers
* from 1 to 10 (Hint : use a variable called product instead of
* of sum and initialize the product to 1 .)
* Based on this code , write a program to display the factorial of n,
* where n is an integer between 1 and 12 .
*/
#include<iostream>
using namespace std;

int main ()
{
int times ;



int sum = 0 ;
int product = 1 ;
while (product <= 10)
{
sum = sum + product ;
times = times * sum ;

product++;
cout<<times<<endl;

}

cout << "";



}
Last edited on
To see what's going on, make a table with one column for each of the variables. Each row will be the value of the variables when executing the "while" statement. Fill in the table and you'll see what's happening.
Product: the answer when you multiply numbers
5 * 5 = 25, so 25 is the product

Sum: the answer when you add numbers
5 * 5 = 10, so 10 is the sum

This program is asking for the sum. In order to get the sum, you add products. In order to get the products, you multiply the numbers.

You need 3 variables: sum, product, number

Start with the bottom: the number. The question states "Modify the program to compute the product of the numbers from 1 to 10". This tells you to start with 1, therefore begin the program with int number = 1;. sum starts with nothing, so we add the next line int sum = 0;. The value of product doesn't matter yet, so the third line can be int product;. So far we have:
1
2
3
4
5
6
7
8
9
10
#include <iostream>

using namespace std;

int main ()
{
    int number = 1;
    int sum = 0;
    int product;
}

Next we need the loop. As you remember "Modify the program to compute the product of the numbers from 1 to 10". This tells you to stop when number reaches 10. As a result the loop will be while (number <= 10). Here is how it should look now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

int main ()
{
    int number = 1;
    int sum = 0;
    int product;

    while (number <= 10)
    {
        
    }
}

If you remember, we need 3 things: sum, product, and number. We have number and to get product, we have to multiply number * number. Inside the loop we will write product = number * number;. Finally we need sum. To get sum, we need to add the product. For the second line inside the loop, write sum = sum + product;. We need to do this for 1 to 10, so the third line inside the loop will be number++;. The final code is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int main ()
{
    int number = 1;
    int sum = 0;
    int product;

    while (number <= 10)
    {
        product = number * number;
        sum = sum + product;
        number++;
    }
}

Do you want to know what is going on while the program is working? Add some output like this:
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
#include <iostream>

using namespace std;

int main ()
{
    int number = 1;
    int sum = 0;
    int product;

    while (number <= 10)
    {
        cout << "product = " << number << " * " << number << endl;
        product = number * number;
        cout << "product = " << product << endl << endl;
        cout << "sum = sum + " << product << endl;
        sum = sum + product;
        cout << "sum = " << sum << endl << endl << endl << endl;
        number++;
        cout << "number = " << number << endl << endl;
    }
    cout << "Program finished." << endl << endl;
    
    return 0;
}
Sorry , the answer is 3268800.
The question is asking multiply from 1 to 10 .
And , ya , thanks for teaching .
Finally , I know how the code goes ...

If I declare like this :

int odd = 0;
int number = 0;
while ( number <= 10 )
{ odd = odd + number + 1 ;
odd ++ ;
number ++ ; }

That means odd has already poses 2 values !! Because it says start from "odd" , so "odd "
start adding 1 because of " odd ++ " and then add another 1 from " +1 " ...
Then , the next , " odd " add another 1 first because of "odd++" and then "number ++ " execute , adding " number" to 1 , then plus one equals to 5 !!
The heck , so mess !! ... so tough for learning programming code ....

I have try to solve by coding with :

int odd = 1 ;
int even = 0 ;
int times ;
int number = 0 ;

while (number <=10 )
{
odd = odd + number ;
// and bla bla bla , FAIL !!! Just like failing a exam paper
// I have try to put odd times even , but ...
// the logic of this computer is ... something I haven't accept it yet ..

Maybe I am not suitable for being an good programmer ...
Last edited on
That means odd has already poses 2 values !! Because it says start from "odd" , so "odd "
start adding 1 because of " odd ++ " and then add another 1 from " +1 " ...
Then , the next , " odd " add another 1 first because of "odd++" and then "number ++ " execute , adding " number" to 1 , then plus one equals to 5 !!
The heck , so mess !! ... so tough for learning programming code ....

Ah. I think I see the problem. You aren't interpreting the code correctly. Here is the code again (and slightly reformatted):
1
2
3
4
5
6
7
8
int odd = 0;
int number = 0;
while ( number <= 10 )
{
    odd = odd + number + 1 ;
    odd ++ ;
    number ++ ;
}


The important thing you may be missing is that the code gets executed in a specific order, one line after another. Odd does NOT possess two values at any one time. And it doesn't "start adding 1 because of odd++". This code executes as follows:

First, lines 1 and 2 define variables odd and number and assigned 0 to each of them.

Then line 3 executes for the first time. Since number is zero, the expression is true and execution proceeds to line 5.

Line 5 executes. odd+number+1 is 0+0+1 so it assigns 1 to odd. Note that it evaluates the right side of the assignment first, using the existing value of odd. Then it assigns the value of the expression to the left side, which happens to be odd.

Lines 6 executes, incrementing odd to 2.

Next line 7 executes, incrementing number to 1.

Next the program goes back up and executes line 3 again. At this point number is 2 so the expression is true and execution proceeds to line 5.

Next line 5 executes again. This time odd+number+1 is 2+1+1, so it assigns 4 to odd.

Line 6 executes, incrementing odd to 5.

Line 7 executes, incrementing number to 2.

Next execution goes back to line 3. Number is still less than 10.

Do you see how this works? The lines execute in a specific order.

This time through the loop it sets odd to 5+2+1=8, then increments odd to 9 and increments number to 3.

Next time it sets odd to 9+3+1=13, increments it to 14 and increments number to 4.

And so on until eventually number is 11 when it executes line 3. At that point, the condition is false, and execution goes to the statement after the loop.

I hope this helps.
Thanks ^ dhayden
I understand it .
Topic archived. No new replies allowed.