Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Example - Prime Number

C++ Example - Prime Number

C++ Example C++ Example

A prime number (prime) has infinitely many instances. A prime is defined as a natural number greater than 1 that has no divisors other than 1 and itself; such a number is called a prime.

Example

include

<

iostream

using

namespace

std

;

int

main

(

)

{

int

n

,

i

;

bool

isPrime

=

true

;

cout

<<

"

Enter a positive integer:

"

;

cin

n

;

for

(

i

=

2

;

i

<=

n

/

2

; ++

i

)

{

if

(

n

%

i

==

0

)

{

isPrime

=

false

;

break

;

}

}

if

(

isPrime

)

cout

<<

"

is a prime number

"

;

else

cout

<<

"

is not a prime number

"

;

return

0

;

}

The output of the above program is:



输入一个正整数: 29

是素数

C++ Example C++ Example