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.
#include <iostream>
usingnamespace 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;
}
- 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.
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?
#include <iostream>
usingnamespace 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;
}
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!
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;
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:
#include <iostream>
usingnamespace 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 loopreturn 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