9 Replies - 770 Views - Last Post: 27 March 2010 - 08:11 PM Rate Topic: -----

#1 keeper962  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 39
  • Joined: 23-February 10

NEED HELP with generating numbers 1-1000 and counting which are Divisi

Posted 27 March 2010 - 08:50 AM

I need to generate #1-1000 on the screen and then the computer counts which of those numbers are divisible by 3.

I am able to computer odd numbers I know how to do that but I can't get the rest of the numbers so 1-1000 show.

I have an idea about how to count which are dividable by 3:
if (n%3==0 && n<=1000),// n is the number 
b=b+1;//this would be the count system
cout<<b<<endl;


I am not sure if that works so can someone help me?

Here is the code I have so far:


#include <iostream>
#include <cmath>
#include <iomanip>
int divisible (int&n);

int n;

using namespace std;
 
#include <stdio.h>

int main()
{
    int InputNumber = 0;
    int i, j, k = 0;
    int PreviousNumber = 0;

    cout<<"Input number greater than two"<<endl;
    cin>>InputNumber;

 
    for(PreviousNumber = 0; PreviousNumber <= InputNumber; PreviousNumber++)
    {

      
            if(PreviousNumber % 3 == 0 && PreviousNumber<=1000)
            {
                k++;
            }
        
        if(k == 0)
        {
        cout<<PreviousNumber<<" ";
        }
        k=0;
    }
     if (n%3==0 && n<=1000),// n is the number 
       b=b+1;//this would be the count system
       cout<<b<<endl;
   
    return 0;
}





So Can anyone help??

This post has been edited by keeper962: 27 March 2010 - 09:06 AM


Is This A Good Question/Topic? 0
  • +

Replies To: NEED HELP with generating numbers 1-1000 and counting which are Divisi

#2 keeper962  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 39
  • Joined: 23-February 10

Re: NEED HELP with generating numbers 1-1000 and counting which are Divisi

Posted 27 March 2010 - 09:19 AM

CAN ANYONE HELP???????
Was This Post Helpful? 0
  • +
  • -

#3 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: NEED HELP with generating numbers 1-1000 and counting which are Divisi

Posted 27 March 2010 - 10:03 AM

What are you trying to do? Your problem isn't very clear.

Are you trying to check which numbers between 1 and 1000 are divisible by 3?

All you need here is a for loop and an if statement.

int nrDivByThree = 0;
for(int i = 1; i <= 1000; i++)
{
  if(i % 3 == 0)
  {
    cout << i << endl;
    nrDivByThree++;
  }
}

cout << "# of numbers divisible by three: " << nrDivByThree << endl;


Is that what you are looking for?

This post has been edited by eker676: 27 March 2010 - 10:05 AM

Was This Post Helpful? 0
  • +
  • -

#4 keeper962  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 39
  • Joined: 23-February 10

Re: NEED HELP with generating numbers 1-1000 and counting which are Divisi

Posted 27 March 2010 - 10:08 AM

well I need to display the numbers 1-1000 on the screen and the computer counts which of the 1000 numbers are divisiable by 3 and displays that number. So when you start the program it displays 1, 2, 3, 4..1000 and also displays the number of (out of 1000) are divisable by 3 so if 300 are divisable by 3 out of the 1000 300 is displayed.
Was This Post Helpful? 0
  • +
  • -

#5 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: NEED HELP with generating numbers 1-1000 and counting which are Divisi

Posted 27 March 2010 - 10:12 AM

Well that requires one for loop and an if statement. I don't know what all those variables are for but all you need is one variable plus the counting variable.

int divis = 0;

for(int i = 1; i <= 1000; i++)
{
  cout << i << endl;

  if(i % 3 == 0)
  {
    divis++;
  }
}

cout << "Numbers divisible by three = " << divis << endl;


Was This Post Helpful? 0
  • +
  • -

#6 Guest_keeper962*


Reputation:

Re: NEED HELP with generating numbers 1-1000 and counting which are Divisi

Posted 27 March 2010 - 12:45 PM

Thank You SO MUCH.

I had to fix on thing but I got it the way I need it.
Thank you for helping me.
I have the code but I need to make it so only functions are in int main()
I think this is right. Right?
#include <iostream>
#include <cmath>
#include <iomanip>
double divisible (int&n);

int nrDivByThree=0;

using namespace std;

int main()
{
 divisible(nrDivByThree);
 return 0;
}

double divisible (int &nrDivByThree)
{

for(int i = 1; i <= 1000; i++)
{
  cout << i << " ";
  if(i % 3 == 0)
  {
    nrDivByThree++;
  }
}

cout <<endl<< "# of numbers divisible by three: " << nrDivByThree << endl;
}

Was This Post Helpful? 0

#7 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: NEED HELP with generating numbers 1-1000 and counting which are Divisi

Posted 27 March 2010 - 12:49 PM

Change the return type of "divisible" to "void", since your function does not return anything. And "nrDivByThree" should be a local variable for your function. You don't use it outside your function.
Was This Post Helpful? 0
  • +
  • -

#8 Guest_Guest*


Reputation:

Re: NEED HELP with generating numbers 1-1000 and counting which are Divisi

Posted 27 March 2010 - 12:54 PM

Here it is but what do you mean about nrDivByThree what needs to be changed for that?

#include <iostream>
#include <cmath>
#include <iomanip>
void divisible (int&n);

int nrDivByThree=0;

using namespace std;

int main()
{
 divisible(nrDivByThree);
 return 0;
}

void divisible (int &nrDivByThree)
{

for(int i = 1; i <= 1000; i++)
{
  cout << i << " ";
  if(i % 3 == 0)
  {
    nrDivByThree++;
  }
}

cout <<endl<< "# of numbers divisible by three: " << nrDivByThree << endl;
}

Was This Post Helpful? 0

#9 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: NEED HELP with generating numbers 1-1000 and counting which are Divisi

Posted 27 March 2010 - 07:58 PM

Using global variables, variables that can be accessed throughout the program, is bad practice when a local variable will suffice. A local variable goes out of scope or deletes itself when it comes to the end of a function/loop/etc.

On to your program,

Look at the comments:
#include <iostream>
#include <cmath>
#include <iomanip>

// Change your function prototype and def.
void divisible ();

// int nrDivByThree=0; <--- You don't need a global variable

using namespace std;

int main()
{
 //divisible(nrDivByThree);
 divisible();
 return 0;
}

void divisible ()
{
  int nrDivByThree = 0; // <---- ADD VARIABLE HERE

for(int i = 1; i <= 1000; i++)
{
  cout << i << " ";
  if(i % 3 == 0)
  {
    nrDivByThree++;
  }
}

cout <<endl<< "# of numbers divisible by three: " << nrDivByThree << endl;
}

This post has been edited by eker676: 27 March 2010 - 08:02 PM

Was This Post Helpful? 0
  • +
  • -

#10 keeper962  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 39
  • Joined: 23-February 10

Re: NEED HELP with generating numbers 1-1000 and counting which are Divisi

Posted 27 March 2010 - 08:11 PM

Thank You for cleaning up my program and for helping me I really appreciate it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1