Prime number check


#include<iostream>
#include<conio.h>
#include<string>

using namespace std;
long num;

string isPrime(long);

main(){
       cout<<"Enter a number: ";
       cin>>num;
       cout<<"\n\nIs prime? " << isPrime(num);      
              
getch();     
}

string isPrime(long x){
     
    string prime = "false";
    
     for(int i=2; i*i < x; i++) //no need to divide by all numbers http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes 
     {
             prime = (x % i ==0) ? "false": "true";
     }
             
     return prime;        
                  
}
Advertisements

About praveenaj

I'm just an average guy who enjoys doing Programming/Web Designs/Pencil art & drinking coffee :)
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

3 Responses to Prime number check

  1. Nice work man.. What is da functionality of dis string header ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s