Saturday, December 17, 2011

Prime Numbers program

Prime Numbers program
This program determines if a number is prime. Click here for the program's source file.
#include <iostream>

using namespace std;

int main()

{

      int num=0;

      int count=0;

      int prime=1;

      char ans=' ';

      do

      {

            cout <<"Enter a number:  ";

            cin >> num;

            for (int i=(num)/2; i>1; i--)

            {

                  if (num%i==0)

                  {

                        count=count+1;

                        if (count==1)

                              cout <<"Internal Factor(s):  ";

                        prime=0;

                        cout <<i<<" ";

                  }

            }

            if (prime==0)

            {

                  cout<<"\nNumber is not prime."<<endl;

            }

            if (prime==1)

            {

                  cout <<"Number is prime."<<endl;

            }

            cout<<"Do you wish to find another prime:  ";

            cin>>ans;

            cin.ignore(200,'\n');

            count=0;

            prime=1;

            system("cls");

      }while(ans =='y' || ans=='Y');

      system("pause");

      return 0;

}


0 comments:

Post a Comment