6 Replies - 998 Views - Last Post: 26 March 2010 - 02:46 PM Rate Topic: -----

#1 wmugume2000  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 wmugume2000  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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


Was This Post Helpful? 0
  • +
  • -

#4 muballitmitte  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 wmugume2000  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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;
}


Was This Post Helpful? 0
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • 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)

Was This Post Helpful? 0
  • +
  • -

#7 wmugume2000  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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;  
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1