# sum of numbers

Page 1 of 1

## 6 Replies - 1396 Views - Last Post: 26 March 2010 - 02: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=164356&amp;s=b6d6459e0c628e8cb9eb3081377e080b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 wmugume2000

Reputation: 0
• Posts: 130
• Joined: 18-March 10

# sum of numbers

Posted 26 March 2010 - 12:38 AM

Trying a program that finds the sum of numbers between 200 and 300 excluding multiples of 7.
Error : primary-expression before '=' token

```# include<iostream>
using namespace std;
int main (){
int n1(200), n2(300),n3;
int sum = 200;
int t=7;
for (n1=200; n1<300; n1++)
{
if (n1 % t)= 0;
sum = sum + n1;
}

cout <<  "The sum of the values between 200 and 300 excluding multiples of 7 is :"<<sum;
cout << endl;
system("pause");
return 0;
}//multipleSum.cpp

```

Is This A Good Question/Topic? 0

## Replies To: sum of numbers

### #2 muballitmitte

• D.I.C Regular

Reputation: 174
• Posts: 470
• Joined: 05-November 08

## Re: sum of numbers

Posted 26 March 2010 - 12:43 AM

You should use the == operator not the assignment operator =

```int sum = 0;
...

if (n1 % t == 0)
sum = sum + n1;

```

also you should initialise the sum with 0 since you are starting from 200

This post has been edited by muballitmitte: 26 March 2010 - 12:45 AM

### #3 wmugume2000

Reputation: 0
• Posts: 130
• Joined: 18-March 10

## Re: sum of numbers

Posted 26 March 2010 - 01:16 AM

I have corrected it but my code seems to be giving the wrong answer, I tried for sum of numbers between 1 and 30 excluding multiples of 7. I should have been 70. it gave me 1

```# include<iostream>
using namespace std;
int main (){
int n1(200), n2(300),n3;
int sum = 200;
int t=7;
for (n1=200; n1<300; n1++)
{
if(! n1%7==0)
sum = sum + n1;
}

cout <<  "The sum of the values between 200 and 300 excluding multiples of 7 is :"<<sum;
cout << endl;
system("pause");
return 0;
}//multipleSum.cpp

```

### #4 muballitmitte

• D.I.C Regular

Reputation: 174
• Posts: 470
• Joined: 05-November 08

## Re: sum of numbers

Posted 26 March 2010 - 01:26 AM

this should fix it

```#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>

using namespace std;

int main (){
int n1,t = 7;
int sum = 0;

for (n1=1; n1<=30; ++n1)
if (n1 % t == 0) //should not have negated the condition whith !
sum = sum + n1;

cout <<  "The sum of the values between 1 and 30 excluding multiples of 7 is :"<<sum;
cout << endl;
system("pause");
return 0;
}//multipleSum.cpp

```

This post has been edited by muballitmitte: 26 March 2010 - 01:29 AM

### #5 wmugume2000

Reputation: 0
• Posts: 130
• Joined: 18-March 10

## Re: sum of numbers

Posted 26 March 2010 - 02:26 AM

My code is now doing the right thing, however I want to reduce the space in between the out characters **
to make it ***************
***********
********
*****
***
*

```# include<iostream>
# include <cstring>
using namespace std;
int main ()
{
cout << endl;
for (int i=18;i>0; i--)
{
for (int k=0; k<i; k++)
cout << "*";
//cout << endl;
for (int j=0; j<i; j++)
cout << endl;
}
//cout << endl;

system("pause");
return 0;
}

```

### #6 japanir

• jaVanir

Reputation: 1014
• Posts: 3,025
• Joined: 20-August 09

## Re: sum of numbers

Posted 26 March 2010 - 03:00 AM

well, probably you get empty lines between your **.
the reason is that loop:
```for (int j=0; j<i; j++)
cout << endl;
```

i don't see a reason why you should loop and print new lines.
you should print only one new line for each iteration of the main loop: so you can just remove that loop.
here is the updated code:
```# include<iostream>
# include <cstring>
using namespace std;
int main ()
{
cout << endl;
for (int i=18;i>0; i--)
{
for (int k=0; k<i; k++)
cout << "*";

cout << endl;
}

system("pause");
return 0;
}
```

also, if you want to print only odd number of characters in a line, you may want to change the main loop to this:
```for (int i=18;i>0; i-=2)
```

### #7 wmugume2000

Reputation: 0
• Posts: 130
• Joined: 18-March 10

## Re: sum of numbers

Posted 26 March 2010 - 02:46 PM

Infact this does exactly what I want except that I want to align it inform of a pyramid.

```# include<iostream>
# include <cstring>
using namespace std;
int main ()
{
cout << endl;
for (int i=18;i>4; i-=2)
{ cout << "\t";
for (int k=0; k<i; k++)

cout << "*";

cout << endl;
//cout << "\n";
}

system("pause");
return 0;
}

```