473,386 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Sum of square integers while loop

I am stumped! I am trying to create a program that computes the sum of square integers from 1 to n. I figured out how to find the sum of integers but the square part is absent to me! Can anyone help me!


int main()
{
int n, i, sum;

cout << "Enter a value for n: ";
cin >> n;
sum = 0;
i = 1;
while (i <= n)
{
sum += i;
i++;
}
cout << "The sum of the first " << n
<< " numbers is " << sum << endl;



return 0;
}
Dec 15 '06 #1
2 13906
Ganon11
3,652 Expert 2GB
I am stumped! I am trying to create a program that computes the sum of square integers from 1 to n. I figured out how to find the sum of integers but the square part is absent to me! Can anyone help me!


Expand|Select|Wrap|Line Numbers
  1. int main() 
  2. {
  3.     int n, i, sum;
  4.  
  5.     cout << "Enter a value for n: ";
  6.     cin >> n;
  7.     sum = 0;
  8.     i = 1;
  9.     while (i <= n)
  10.     {
  11.         sum += i;
  12.         i++;
  13.     }
  14.     cout << "The sum of the first " << n
  15.     << " numbers is " << sum << endl; 
  16.  
  17.     return 0;
  18. }
  19.  
Right now, you are using "sum += i" which will add the integer i to sum. You say you want to add the square of i instead of i (i.e. i * i). So shouldn't you update that line to

Expand|Select|Wrap|Line Numbers
  1. sum += i * i;
Dec 15 '06 #2
Right now, you are using "sum += i" which will add the integer i to sum. You say you want to add the square of i instead of i (i.e. i * i). So shouldn't you update that line to

Expand|Select|Wrap|Line Numbers
  1. sum += i * i;


Thanks so much! It hit me just as I started to read what you wrote. I so Appreciate your help!!!!!!
Dec 17 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: cplusplus | last post by:
Hello, I have newbie question. I'm stuck on this current assignment. Write a program that prompts the user for two integer values, passes the values to a function where they are multiplied...
13
by: Nicholas | last post by:
How can I compare char* with integers and characters contained in the str, where integers can be one digit or more? void Access(char *str) { char *pt = str; while (pt != '0') { if...
16
by: aruna | last post by:
Given a set of integers, how to write a program in C to sort these set of integers using C, given the following conditions a. Do not use arrays b. Do not use any comparison function like if/then...
2
by: Clint Olsen | last post by:
Hello: I posted a thread on comp.programming awhile back asking about an algorithm I implemented on square root. The idea was to use the square root of a prime number as a convenient way to get...
11
by: Tim S Roberts | last post by:
If I have a value, say 13849, I can assign it to x and test if it divisible by 37 by something like if (x%37==0)... and I can test if it's a square by something like int y; y=sqrt(x); if...
5
by: punkybrewster | last post by:
My task is to write a program that reads in from the user the size of the sides of a square and prints a hollow square of that size in asterisks and spaces. A nested loop should be done. An example...
3
by: triplejump24 | last post by:
Hey. Im trying to make program that basically displays all the prime numbers. I need to use bool and for but im not quite sure if i have this right. So far i have this bunch of a mess but can...
3
by: Sejoro | last post by:
Hey again, I'm trying to write a program that will output a random valid Latin Square (9x9 square of numbers 1 - 9 with no repetition in the rows and columns) and can't get my numbers to not...
8
by: shaarawy | last post by:
The goal of this problem is to write a program which will take from 1 to 5 puzzle pieces such as those shown below and arrange them, if possible, to form a square. The pieces cannot be rotated or...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.