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.