Help:The for loop-sum of cubes between 2 and n!

Okay I need a program where the user chooses a number greater than 2, then the program gives the sum of the cubes of all the even numbers between 2 and n, prints the results on screen.

However this is returning the wrong answers?

Thank you in advance!

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
#include <iostream>

using namespace std;

int main()
{
    int n, a;
    int sum;
    int temp;

    cout << "Choose a number greater than 2 and press enter:" << endl;
    cin >> n;



    for (int a=2; a<=n; a++)
        {

        int temp = 0;
        temp= (a*a*a) + (n*n*n);
        }

        sum=temp;
        cout << "The cube of all even numbers between 2 and the chosen numbers is" << sum << endl ;

    return 0;

}
closed account (o3hC5Di1)
Hi there,

Some suggestions:

- You are not checking whether the use has entered a number higher than two.
- You are not checking whether a is an even number.
- I'm not sure your formula (a*a*a) + (n*n*n) is correct. You say:

the program gives the sum of the cubes of all the even numbers between 2 and n


You just have to add the cube of the current even number to the total it seems to me?

- In every iteration of your for-loop, you reset tmp to zero. only when all the numbers have finished do you copy tmp into sum. Why not just add to sum in every iteration? sum += (a*a*a);


Hope that helps, please do let us know if you require any further help.

All the best,
NwN
Hi,

Thanks for your advice.

A is always 2, so is there a need to check whether it's even or not?
With regards checking n is greater than 2, what function do you use?

As for the (a*a*a) + (n*n*n) equation, will it not keep updating as long as it is greater than 2, an even number, and does not exceed n?

This is my third week of programming so I apologise if this is all simple stuff!
closed account (o3hC5Di1)
Hi,

amc246 wrote:
With regards checking n is greater than 2, what function do you use?


A simple if statement should suffice:

1
2
3
4
5
6
7
8
9
10
if (n > 2)
{

    for (int a=2; a<=n; a++)
    // ...
}
else
{
    std::cout << "Invalid input.";
}



Since a is always even, you could just increment it by two within your for loop:

for (int a=2; a<=n; a+=2)

Hope that helps.

All the best,
NwN
Last edited on
#include <iostream>

using namespace std;

int main()
{
int n, a;
int sum;
int temp;

cout << "Choose a number greater than 2 and press enter:" << endl;
cin >> n;

if (n > 2)
{

for (int a=2; a<=n; a++)
{
if ( (a%2) == 0)
int temp = 0;
temp= (a*a*a) + (n*n*n);
}
else
{
std::cout << "Invalid input.";
}


sum=temp;
cout << "The cube of all even numbers between 2 and the chosen numbers is" << sum << endl ;

return 0;

}

I now understand that I have 4 ints-a,n,sum and temp. The user is asked to choose a number greater than 2, it is then checked to be greater than 2 and a is checked to be an even number and if so the user gets the sum of all even numbers cubed between 2 and n. If n was not greater than 2 then 'invalid i intput' would be printed on screen. However if it was, it would be printed on screen?

Am I in anyway on track or should I begin again?
like this

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main(void) {
	while (1) {
		int n, a=0;
		std::cin >> n;
		for (int i=4; n>i; i+=2) {
			a+=i*i*i;
		}
		std::cout << a << std::endl;
	}
}
Last edited on
closed account (o3hC5Di1)
You are on the right track, please allow me to do some more suggestions:

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
#include <iostream>

using namespace std;

int main()
{
int n, a;
int sum;
int temp; //not needed

cout << "Choose a number greater than 2 and press enter:" << endl;
cin >> n;

if (n > 2)
{

for (int a=2; a<=n; a+=2) //just add 2 to a, that makes even numbers just as much.
{
    sum += (a*a*a) + (n*n*n); //increment sum rather then a tmp
}
else
{
    std::cout << "Invalid input.";
}


sum=temp; //not needed
cout << "The cube of all even numbers between 2 and the chosen numbers is" << sum << endl ;

return 0;

}


Hope that helps.

All the best,
NwN
Last edited on
NwN

#include <iostream>

using namespace std;

int main()
{
int n, a;
int sum;
int temp; //not needed

cout << "Choose a number greater than 2 and press enter:" << endl;
cin >> n;

if (n > 2)
{

for (int a=2; a<=n; a+=2) //just add 2 to a, that makes even numbers just as much.
{
sum += (a*a*a) + (n*n*n); //increment sum rather then a tmp
}
else
{
std::cout << "Invalid input.";
}


sum=temp; //not needed
cout << "The cube of all even numbers between 2 and the chosen numbers is" << sum << endl ;

return 0;

}
Thank you so much, makes much more sense to me now, only issue is when i try to build that it says 'else' without previous 'if'? Other than that, simple to understand and flawless!
closed account (o3hC5Di1)
Hi there,

Sorry, I made a little mistake:

1
2
3
4
5
6
7
8
9
10
11
12
13
if (n > 2)
{

    for (int a=2; a<=n; a+=2) 
    {
        sum += (a*a*a) + (n*n*n);
    }

}// Add curly brace here
else
{
std::cout << "Invalid input.";
}


All the best,
NwN
Last edited on
The program runs perfectly but returns an answer of 1967508 instead of 288 when n=6, for example? Reading it, it makes sense to me however
closed account (o3hC5Di1)
Hi,

Like I told you earlier: sum += (a*a*a) + (n*n*n); doesn't seem like calculating the cube.

I believe you should only need: sum += (a*a*a);.

That should fix it :)

All the best,
NwN
You were right, I'm nearly there.

How can I get this loop to restart and disregard my first answer when i selct 'y' at the end, it seems to add my new result to my old and just keep going?

#include <iostream>

using namespace std;

int main()
{
int n, a;
int sum = 0;
char ch = 'y';


while( ch == 'y' )
{
cout << "Choose a number greater than 2 and press enter:" << endl;
cin >> n;

if (n > 2)
{ for (int a=2; a<=n; a+=2)
{
sum += (a*a*a);
}
cout<< " " << endl << endl;

cout << "The cube of all even numbers between 2 and the chosen numbers is" << sum << endl ;

}

else
{
std::cout << "Invalid input.";
}
cout<< " " << endl << endl;
cout <<"Would you like to do this again with a different number? (y/n)"<< endl;
cout<< " " << endl << endl;

cin >> ch;
}

return 0;

}
The program runs perfectly but returns an answer of 1967508 instead of 288 when n=6, for example? Reading it, it makes sense to me however


The only number that is between 6 and 2 and even is 4 and 4^3 is 64. So if n=6, f(n)=64.
Last edited on
closed account (o3hC5Di1)
Hi there,

Please wrap your code in [code][/code]-tags, it makes it much more readable.
You have the basic idea, but you've misplaced your loop a little bit. Also, use a "do/while" loop to ensure that the first time it is always executed, no matter if the condition holds up:

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
#include <iostream>

using namespace std;

int main()
{
int n, a;
char ch;


do //start do.while loop
{
cout << "Choose a number greater than 2 and press enter:" << endl;
cin >> n;

if (n > 2)
{ for (int a=2; a<=n; a+=2)
{
sum += (a*a*a);
}
cout<< " " << endl << endl;

cout << "The cube of all even numbers between 2 and the chosen numbers is" << sum << endl ;

}

else
{
std::cout << "Invalid input.";
}
cout<< " " << endl << endl;
cout <<"Would you like to do this again with a different number? (y/n)"<< endl;
cout<< " " << endl << endl;

cin >> ch;
}

} while(ch == 'y' || ch =='Y');  //end the do/while loop

return 0;

}


Make sure to take MarketAnarchist's comment in mind, if the assignment says "between", 2 and n should probably be excluded from the calculation.
Also, indentation makes your code a lot more readable, for more information: http://en.wikipedia.org/wiki/Indent_style

All the best,
NwN
Last edited on
Topic archived. No new replies allowed.