HELP PLEASE!!!

How to make program that scan a number n and then output the sum of the squares from 1 to n. Thus, if the input is 4, the output should be 30 because:

12 + 22 + 32 + 42 = 1+4+9+16 = 30

Using for, while, do while looping statements.
Last edited on
You only need one type of loop (for).
Aside from that, what's the problem?
12+22+32+42 = 1+4+9+16 = 30
That supposed to be 1²+2²+3²+4² = 1+4+9+16 = 30
I'm just new in C++ and I can't get the exact code for that using the three statements.
1. Prompt for the number.
2. Write a for loop in which set first condition to 0 and second to n.
3. Square a variable(say j) initially set to 0 and add it to the another variable(say sum).
4. increment the variable(j).
5. print the sum when out of for loop.

For more help: http://recurseit.blogspot.com

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream.h>

int scanSquares(int input)
{
  int output = 0;
  while (input > 0)    output += input * input--;
  return output;
}

int main()
{
  int ScanMe;
  cout << "Scan which number? ";
  cin >> ScanMe;
  cout << endl << "The scanned number is: " << scanSquares(ScanMe);
}
Last edited on
oh! it is the right code for that problem?
It hardly matters. If you're taking a programming class, you should drop it now.
With your current level of commitment you will fail it either way.
Agreed with Athar...
It is not that difficult to create a program once you get the algorithm. Focus on learning. I am sure you will start writing such easy programs within a week...
Sorry, but I don't have a programming class. I only study with my own, no one is teaching me. NO examples, NO explanations at all. I'm just reading on the internet and trying to have a guide. It's ok if no one can help me.

THANK YOU FOR YOUR COMMENTS...
Then what you need is such a guide, a.k.a. a book.
The C++ Primer is a good book and if you want a free one, you can read "Thinking in C++" on the web:
http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html
Thanks.
Topic archived. No new replies allowed.