hello ;

here is a quastion . i understand it until the red senence , i do not know what dose it mean mathematically ?

Write a program that prompts the user to input a positive integer. It should then output indicating whether the number is a prime.

Note: an even number is a prime if it is 2. And an odd integer is prime if it is not divisible by any odd integer less than or equal to its square root.

## All 9 Replies

hello ;

here is a quastion . i understand it until the red senence , i do not know what dose it mean mathematically ?

Write a program that prompts the user to input a positive integer. It should then output indicating whether the number is a prime.

Note: an even number is a prime if it is 2. And an odd integer is prime if it is not divisible by any odd integer less than or equal to its square root.

Mathematically, any non-prime number will have a factor less than or equal to its square root. That means that if you have established that a number has no factor less than or equal to its square root, you have established that it is a prime number. Take 103 for example. The square root is about 10.15. Since factors must be integers, round down to 10. So to show that 103 is prime, you show that it has no factors less than or equal to 10. Further, you only have to test prime numbers. Hence, prove that 2, 3, 5, and 7 are not factors and you have proven that 103 is not prime. Thus you don't need to bother testing 4, 6, 8, and 10 since they are multiples of 2. You also don't need to test 9 since it's a multiple of 3.

``````#include<iostream>
using namespace std;
int u=0,x=0;
int main()
{
int n=0;
cout<<"Enter a number...";
cin>>n;
if(n==0|n==1){
cout<<"You entered a 0 or a 1. You know that 0 is not a prime and 1 is a prime!!"<<endl;
return 0;
}
if(n==2){
cout<<"This is the only even prime number!!"<<endl;
return 0;
}
if(n%2==0){
cout<<"The number entered is not a prime number!!"<<endl;
return 0;
}
while(u<n*n){
if(u%2!=0){
if(n%u!=0)
x=1;
}
else{
x=0;
}
u++;
}
if(x==1)
cout<<"The number entered is not a prime number!!"<<endl;
else
cout<<"This is a prime number!!"<<endl;
return 0;
}``````

see this simple code , there is an error , but i can not solve it !

``````#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
using namespace std;
void main()
{
int  num, i=1;
cout<<"Enter num:";
cin>>num;
if (num==1||num==2)
cout<<"Prime , you enter 1 or 2";
else if (num%2==0)
cout<<"Not prime";
else
{int t=sqrt(num);
while (i<=t)
{ if (num%i==0)
cout<<"Not prime";break;
else
i++;
}
}

}``````

see this simple code , there is an error , but i can not solve it !

``````#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
using namespace std;
void main()
{
int  num, i=1;
cout<<"Enter num:";
cin>>num;
if (num==1||num==2)
cout<<"Prime , you enter 1 or 2";
else if (num%2==0)
cout<<"Not prime";
else
{int t=sqrt(num);
while (i<=t)
{ if (num%i==0)
cout<<"Not prime";break;
else
i++;
}
}

}``````

The formatting/indentation makes this code very hard to read. Compare with this:

``````#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
using namespace std;

void main()
{
int  num, i=1;

cout<<"Enter num:";
cin>>num;

if (num==1||num==2)
{
cout<<"Prime , you enter 1 or 2";
}
else if (num%2==0)
{
cout<<"Not prime";
}
else
{
int t=sqrt(num);

while (i<=t)
{
if (num%i==0)
cout<<"Not prime";
break;
else
i++;
}
}
}``````

Notice that there's no cout statement of "prime" except for 1 and 2 (1 is NOT prime). How do you know whether 11 is prime? There's no display of "prime" when all tests are passed.

see this simple code , there is an error , but i can not solve it !

``````#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
using namespace std;
void main()
{
int  num, i=1;
cout<<"Enter num:";
cin>>num;
if (num==1||num==2)
cout<<"Prime , you enter 1 or 2";
else if (num%2==0)
cout<<"Not prime";
else
{int t=sqrt(num);
while (i<=t)
{ if (num%i==0)
cout<<"Not prime";break;
else
i++;
}
}

}``````

The formatting/indentation makes this code very hard to read. Compare with this:

``````#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
using namespace std;

void main()
{
int  num, i=1;

cout<<"Enter num:";
cin>>num;

if (num==1||num==2)
{
cout<<"Prime , you enter 1 or 2";
}
else if (num%2==0)
{
cout<<"Not prime";
}
else
{
int t=sqrt(num);

while (i<=t)
{
if (num%i==0)
cout<<"Not prime";
break;
else
i++;
}
}
}``````

Look closely at line 32 It now stands out more clearly. Brackets?

On another note, notice that there's no cout statement of "prime" except for 1 and 2 (1 is NOT prime). How do you know whether 11 is prime? There's no display of "prime" when all tests are passed successfully.

^^^^^
ok , i will bt such statement for printing the primes ,
but it still has an error saying :
'sqrt' : ambiguous call to overloaded function

^^^^^
ok , i will bt such statement for printing the primes ,
but it still has an error saying :
'sqrt' : ambiguous call to overloaded function

I don't get that error. I get a conversion warning that went away when I did this:

``int t = (int) (ceil(sqrt(num)));``

http://www.cplusplus.com/reference/clibrary/cmath/sqrt/

It might possibly not like the fact that num is an integer. I suppose if you still have a problem, typecast it to a double.

``int t = (int) (ceil(sqrt((double) num)));``

thank U very much , it is work now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.