Help me with my C++ code pls.

Pages: 12
a program that will read a number, n, and then output the sum of the squares of the numbers from 1 to n, So if the input is 3, the output should be 14, because:
1^2+2^2+3^2=1+4+9=14
See the other thread you created. We don't solve homework problems: you'll have to give us proof that you tried to solve the problem yourself before we'll give you any code help. :)

A hint: Use a for loop and the += operator.

-Albatross
On the other hand, 1^2+2^2+3^2+...+n^2=n*(n+1)*(2*n+1)/6, so you don't need to even implement a loop.

(14= 3*(3+1)*(2*3+1)/6)
hello there i have the same problem with iNgine. this is my code
#include <iostream>
using namespace std;
int main()

{
int i,sum,n,a,compute;
sum=0;
n=0;

cout<<"\nEnter an Integer/Number: ";
cin>>n;

for(i=1;i<=10;i++)
{
sum=(i*i)+sum;
}





system("pause");
cin.ignore();
cin.get();

return 0;
}











please help me to solve that problem.

You're almost done:

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
#include <iostream>
using namespace std;
int main()

{
int i,sum,n,a,compute;
sum=0;
n=0;

cout<<"\nEnter an Integer/Number: ";
cin>>n;

for(i=1;i<= n;i++) // Note: n instead of 10
{
sum=(i*i)+sum; // You can also write sum+=(i*i);
}

// I think you want to see the output
cout<<"\nSum: " << sum << endl;




system("pause"); // Not needed
cin.ignore();
cin.get();

return 0;
}
Last edited on
is that the code already? i really need to solve this 3 problems. because the deadline is tomorrow :( but i will try it first. BTW thanks
yey i got the answer its 14. but how about the solution? thanks a lot. thank you so muuch. :))))
When I start getting homework can you do mine too.
but how about the solution?
I don't know what you mean. If that solves your problem it's supposed to be the solution? 3 problems?
what i mean is i think i need also a solution that will appear in the output?? on how the answer is to be like that. if ever you dnt get what im telling. its ok. :))) nevermind. ill try the second problem. :))
please help me with these problem??


2.) Assume a range of integer values starting from N1 and ending at N2. Assume also
an integer say M. Write
. You are also required to display the count of
such numbers for example, if N1 = 4, N2 = 12 and M = 4, the output will be 4, 8,
12. The value 3 will also be displayed indicating that there are 3 numbers which
are divisible by 4.

PLEEEASEE?? help me? im so sorry i just really need to pass this tomorrow. :(
It's not considered good practice on here to just to give out full solutions. Have you made any attempt so far which you can show us? If not, consider using a for loop (or a while loop if you'd prefer).

HINT: Remember that the counter variable in a for loop doesn't have to increase in steps of 1.
Last edited on
im sorry. ill try it. but what if i dont make it? would you help me to solve this problem??
You don't have to say sorry - I wasn't criticising. I was just saying that I'm not comfortable with just giving you the answer.

Think about using a for loop whose counter variable doesn't necessarily start from zero and isn't necessarily incremented by 1 each time.

Another hint: you have three inputs - N1, N2, M - and three places to put custom variables in a for statement (apart from the counter variable, of course). So think about which variable to put where in the for statement, and post your attempt here if possible.
#include<iostream>
using namespace std;
int main()
{
int n1, n2, M, n;
n1=0;
cout<<"Enter first Number: ";
cin>>n1;
cout<<"Enter Second Number: ";
cin>>n2;
cout<<"Enter Third Number: ";
cin>>M;
cout<<n1 <<n2 <<M;
for(n=0;n<=m;n++)




cin.ignore();
cin.get();
return 0;
}

how bout this? can you help me with this? im not good in programming thats why i know some variables are not on their right places where they should be.
OK. So you've structured the general program correctly and put the loop in the right place. However, the syntax of your loop is not correct.

(Have a read about for loops here, if necessary. http://cplusplus.com/doc/tutorial/control/#for )

Then think about what you want your for loop counter variable to do. It should start at N1 and go to N2, taking steps of size M. Finally, you will need to add some code to execute with each loop iteration. Reading about for loops at the link above will help you.

Hope this helps.
Last edited on
thanks you so much for your help. i really appreciate it. are you still here until tomorrow?? i would ask you again for some that i dont understand. is it ok?? its getting late so ill just continue this tomorrow. thanks again fo the help. i need to study for my exams tomorrow. :))) i hope i can still ask you tomorrow morning. thanks muuch. :)) it helps me a lot.
Well I'm on quite a bit at the moment. I don't know what time zone you are in, but I am in GMT+0 so I'll be here for another 10 or 11 hours.
joysiejoyce wrote:
what i mean is i think i need also a solution that will appear in the output??
Did you see line 18/19
1
2
// I think you want to see the output
cout<<"\nSum: " << sum << endl;
?
yes. i saw it. :))))) we're already solving number 2 in our problem. :)))))

this.
#include<iostream>
using namespace std;
int main()
{
int n1, n2, M, n;
n1=0;
cout<<"Enter first Number: ";
cin>>n1;
cout<<"Enter Second Number: ";
cin>>n2;
cout<<"Enter Third Number: ";
cin>>M;
cout<<n1 <<n2 <<M;
for(n=0;n<=m;n++)




cin.ignore();
cin.get();
return 0;
}

how bout this? can you help me with this? im not good in programming thats why i know some variables are not on their right places where they should be.

can you check it??
Pages: 12