Thread: Prime numbers
View Single Post
  #6 (permalink)  
Old 05-12-2008, 06:56 AM
dvdtomkins dvdtomkins is offline
Newbie
 
Join Date: May 2008
Posts: 16
Rep Power: 2
dvdtomkins is on a distinguished road
Default Re: Prime numbers

I just wanted help getting started, anyway I found some help on another site and came up with this code:
Code:
#include <iostream>
using namespace std;


int main()
{
          int n; 
          int i;
          int c=0;
          cout<<"enter a number:";
          cin>>n;

        for(i=1;i<=n;i++) // below is the statements to test for a prime number
         {
           if(n%i==0)
           {
             c += 1;
           }
         }//end of for

       if(c==2)
         {
          cout<<n<<" is a prime number";
         }//end of if

       else
         {
          cout<<n<<" is a composite number it's factors are";    
         for(int k=1; k< n;k++)
            {
              if(n%k==0)
                {
                 cout<<","<<k;                  

                }//end of if


            }//end of for        
 
 

         }//end of else

  return 0;
}// end of mainA

Last edited by WingedPanther; 05-12-2008 at 11:45 AM. Reason: add code tags
Reply With Quote