Prime Number Function (C++)




/* And now i'll share c++ functions :
- a function to check wether a number is a prime or not
- a function to print prime numbers in a certain range
- a main function as usually c++ do */

int primecheck(int n)
{
                int i;

                for (i=2; i<n; i++)
                {
                                if (n % i == 0)
                                                return 0;
                }
                if(n>1)
                                return 1;
                else
                                return 0;
}

void primerange(int startx, int endx)
{
                int z,col;
                for(z=startx;z<=endx;z++)
                {
                                if (primecheck(z)==1)
                                {
                                                cout<<z<<"\t";
                                                col++;
                                                if(col==10)
                                                {
                                                                cout<<"\n";
                                                                col=0;
                                                }
                                }
                }
}

void main()
{
                int x;
                int startx,endx;
                cout<<"Number to test : ";
                cin>>x;
                if (primecheck(x)==1)
                                cout<<x<<" is a prime";
                else
                                cout<<x<<" isn't a prime";
                cout<<"\n\nEnter a start number of prime range : ";
                cin>>startx;
                cout<<"Enter an end number of prime range : ";
                cin>>endx;
                primerange(startx,endx);
                getch();
}


//coded by Radian Baratasena, for educational purpose only

Comments

Popular posts from this blog

Hot Keyboard Pro 3.2.700 Incl Serial Key

Borland Turbo C++ 4.5 download