# Finding the sum of squared odd integers till 333

Page 1 of 1

## 5 Replies - 6885 Views - Last Post: 16 April 2012 - 04:46 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=275496&amp;s=2f7c32a749b6824f58d522b7654c0914&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Houston573

Reputation: 0
• Posts: 46
• Joined: 14-April 12

# Finding the sum of squared odd integers till 333

Posted 16 April 2012 - 02:17 PM

I'm trying to write a program which sums the squares of the first 333 positive odd integers.
```int main()
{
//declare variables
int square=2;
int sum;
int total=0;
int count=0;

while (total <= 333)
{
if (square % 2 == 1)
{
count = count + 1;
total = square * square;
count ++;
square ++;
}
else
{
cout<<"Use only odd integers! ";
}
}

sum = total + (square * square);

return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: Finding the sum of squared odd integers till 333

### #2 DimitriV

• vexing conundrum

Reputation: 587
• Posts: 2,746
• Joined: 24-July 11

## Re: Finding the sum of squared odd integers till 333

Posted 16 April 2012 - 02:24 PM

What's wrong? Does it output incorrectly?
You set total to only square * square every time. Is this what you want? Wouldn't you be adding that value to total instead?
```total += (square * square)
```

### #3 raspinudo

Reputation: 61
• Posts: 232
• Joined: 19-September 11

## Re: Finding the sum of squared odd integers till 333

Posted 16 April 2012 - 02:38 PM

Wouldn't you want your while loop to check count, not total? If I read the problem correctly, you want to add the squares of the first 333 positive odd integers. i.e. 1^2 + 3^2 .... You could have a temp variable that is always incremented each loop. In this way, if an odd is found, increment count, and sum = (temp*temp)

```int main()
{
//declare variables;
int sum = 0; // keeps track of our total
int temp = 0; // this is our incremental variable
int count = 0; // this will keep track of how many odd numbers we've found

while (count < 333) //while we haven't found 333 odd numbers
{
temp++;
if (temp % 2 == 1) //if num is odd
{
count++; //inc. total odd nums found
sum += (temp*temp); //add the square of curr number to sum
}
}
return 0;
}

```

This post has been edited by raspinudo: 16 April 2012 - 02:56 PM

### #4 Houston573

Reputation: 0
• Posts: 46
• Joined: 14-April 12

## Re: Finding the sum of squared odd integers till 333

Posted 16 April 2012 - 04:26 PM

raspinudo, on 16 April 2012 - 02:38 PM, said:

Wouldn't you want your while loop to check count, not total? If I read the problem correctly, you want to add the squares of the first 333 positive odd integers. i.e. 1^2 + 3^2 .... You could have a temp variable that is always incremented each loop. In this way, if an odd is found, increment count, and sum = (temp*temp)

```int main()
{
//declare variables;
int sum = 0; // keeps track of our total
int temp = 0; // this is our incremental variable
int count = 0; // this will keep track of how many odd numbers we've found

while (count < 333) //while we haven't found 333 odd numbers
{
temp++;
if (temp % 2 == 1) //if num is odd
{
count++; //inc. total odd nums found
sum += (temp*temp); //add the square of curr number to sum
}
}
return 0;
}

```

Thank you for your help. I think that's the basic structure of the program except i had to use a cout << sum; at the end, but no big deal. The problem is worded funny and i think my teacher is looking for 333 odd integers squared and then sum up all of them which is how this program works. I'm very new to this and i've never seen some of the stuff you used for example, sum += (temp * temp). I do have one more question though...is there a way to check if it's only summing the squares of odd integers?
I thought about setting up my temp=1 and then in my while statement using temp+2;

### #5 DimitriV

• vexing conundrum

Reputation: 587
• Posts: 2,746
• Joined: 24-July 11

## Re: Finding the sum of squared odd integers till 333

Posted 16 April 2012 - 04:34 PM

I'm imagining that that could work: checking the value that % returns on it should be enough.

### #6 Houston573

Reputation: 0
• Posts: 46
• Joined: 14-April 12

## Re: Finding the sum of squared odd integers till 333

Posted 16 April 2012 - 04:46 PM

DimitriV, on 16 April 2012 - 04:34 PM, said:

I'm imagining that that could work: checking the value that % returns on it should be enough.

Well i tried it that way, but doesn't produce the same number. I think this way is much better and works because the if statement makes sure that the number has to be odd and then adds to it and continues to add until 333 odd integers have been squared and added together.
```#include <iostream>
#include <string>

using namespace std;

/*
*
*/
int main()
{
//declare variables
int sum = 0;
int temp = 0;
int count = 0;

while (count <= 333)
{
temp++;
if (temp % 2 ==1)
{
count++;
sum += (temp * temp);
}

}

cout << "The sum is: "<< sum<<endl;

return 0;
}

```

This post has been edited by jimblumberg: 17 April 2012 - 05:44 AM
Reason for edit:: Fixed Code tags.