# Stuck

• 01-05-2005
RenderedAwake
Stuck
Ive just started learning c++ and I've been working on writing a program for over a week now, at this point I've pretty much lost hope and have no clue what to do. Im trying to write a program that will list out numbers 1-50 along with their factors and whether they are prime or not, so basically Im trying to create a sieve of erathsothenes thing that will predict whether the numbers are prime or not, and do it with the basic stuff like loops and if statements, ect..does anyone have any suggestions?

this is how far ive gotten:....

Code:

``` #include <iostream> #include <math.h> double numb; double factor; double leftover; double numoffactors; int main() {         using namespace std;         cout << "Number          Factor          Prime or Not" << endl;          cout<< " ------------------------------------------"<< endl;         cout<<  " 1              doesn't count      Not Prime    "<< endl;         cout.width(10);         for(int numb=2;numb <=20;numb++)                        {         cout << numb << endl;         cout.width(19);         cout<< factor<<endl;         cout.width(39);         cout<<leftover<<endl; } for(int factor = 2;factor<=20;factor++)                  while (numb>factor)         {         leftover= fmod(numb,factor);  }             if(leftover == 0)                 {                         numoffactors = (factor + 1);                                 }                 else                         {                         numoffactors=1;                         }                                                 if(numoffactors < 2)                 {                         cout.width(35);                         cout << "Prime" << endl;                 }                                                                 if(numoffactors > 2)                 {                         cout.width(35);                                cout << "Not Prime" << endl;                 }                                 return 0; }```
• 01-05-2005
Code:

```#include <iostream> #include <cmath>  //use the up to date math header using namespace std;  //make stuff in namespace std                                           //available throughout program int main() {   //avoid global variables whenever possible   //every number you use will be an int so use ints   //not doubles   int numb;   int factor;   int numoffactors;   cout << "Number                  Factor                  Prime or Not" << endl;    cout<< " ------------------------------------------"<< endl;   cout<<  " 1                          doesn't count          Not Prime        "<< endl;   cout.width(10);   //factor should be given a meaningful value before   //you attempt to use it   //don't redeclare numb and factor in loops   for(numb = 2; numb <= 20; ++numb)  //prefer prefix increments over                                                                         //postfix                                                                         //be generous but not over                                                                         //indulgent with spaces to make                                                                         //the code easier to read   {         cout << numb << endl;         cout.width(19);           numoffactors = 2;                          //all numbs have at least two                                                                         //"factors", 1 and numb                                                                         //set numoffactors to 2 for each                                                                         //time through outer loop         for(factor = 2; factor<= numb; ++factor)                                                                         //don't use 20, use numb and                                                                         //drop a loop         {           if(numb % factor == 0)                //I prefer % to fmod()           {                 cout << factor << ", ";                 numoffactors += 1;                          }         }           cout.width(35);                        //only need this once         if(numoffactors == 2)                  cout << "Prime" << endl;         else                                        //Prime, Not Prime is mutually                                                                   //exclusive so use if/else                                                                   //rather than to ifs           cout << "Not Prime" << endl;   }   return 0; }```
I'll avoid comment on indentation practices because I never know when it's you versus this board screwing you up, but try to be as neat as possible to be sure you have the correct matching {}s and {}s wherever you need them.
• 01-10-2005
RenderedAwake
Thanks a bunch for your help :) ...

I had just one more question on this..when I check numbers 2 through 20 it works and outputs everything just fine but when I get up to numbers like 2 through 500 the output starts looking really messy even though the rows and everything were lined up initially. I dont even necessarily need all the numbers to be displayed but is there a way for me to keep the rows and columns that are displayed lined up?
• 01-11-2005
Kleid-0
Something like this should work with you:
Code:

```if(numb % factor == 0) {         if(factor < 10)                 cout << factor << ",  ";         else if(factor < 100)                 cout << factor << ",  ";         else if(factor < 1000)                 cout << factor << ", ";         numoffactors += 1;                }```
Red = changed.
Basically this code is checking to see how many space the number actually needs, so that all of the numbers could be outputted in a nice straight line, I hope I got what you needed.

--EDIT--
Ack! I was looking at the wrong code, but something like above should give you what you want. Find the number that takes up the greatest amount of characters of space, and then pad the whole column with that set of space padding. Am I making any sense? :(
• 01-11-2005
Scribbler
Quote:

I dont even necessarily need all the numbers to be displayed but is there a way for me to keep the rows and columns that are displayed lined up?
You can use the <iomanip> library to format the output of your code if you're having a problem with layout. You can incorporate the following into your code...

Code:

```#include <iomanip> #define COLWIDTH 10 // Set to whatever colum width you prefer in your table using std::setw; // If you haven't already used 'using namespace std;' cout << setw(COLWIDTH) << factor;```
• 02-02-2005
RenderedAwake
Quote:

Code:

```#include <iostream> #include <cmath>  //use the up to date math header using namespace std;  //make stuff in namespace std                       //available throughout program int main() {   //avoid global variables whenever possible   //every number you use will be an int so use ints   //not doubles   int numb;   int factor;   int numoffactors;   cout << "Number          Factor          Prime or Not" << endl;    cout<< " ------------------------------------------"<< endl;   cout<<  " 1              doesn't count      Not Prime    "<< endl;   cout.width(10);   //factor should be given a meaningful value before   //you attempt to use it   //don't redeclare numb and factor in loops   for(numb = 2; numb <= 20; ++numb)  //prefer prefix increments over                                     //postfix                                     //be generous but not over                                     //indulgent with spaces to make                                     //the code easier to read   {     cout << numb << endl;     cout.width(19);       numoffactors = 2;              //all numbs have at least two                                     //"factors", 1 and numb                                     //set numoffactors to 2 for each                                     //time through outer loop     for(factor = 2; factor<= numb; ++factor)                                     //don't use 20, use numb and                                     //drop a loop     {       if(numb % factor == 0)        //I prefer % to fmod()       {         cout << factor << ", ";         numoffactors += 1;              }     }       cout.width(35);            //only need this once     if(numoffactors == 2)          cout << "Prime" << endl;     else                    //Prime, Not Prime is mutually                                   //exclusive so use if/else                                   //rather than to ifs       cout << "Not Prime" << endl;   }   return 0; }```
I'll avoid comment on indentation practices because I never know when it's you versus this board screwing you up, but try to be as neat as possible to be sure you have the correct matching {}s and {}s wherever you need them.

...k i just went back thro my programs and looked at this again, i had thought i finished it with this but the program only prints out "not primes". ive been through this several times but i cant figure out whats stopping it from identifying and printing out "primes" too.
• 02-02-2005