Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Use C++ to write a function to determine whether a number is a prime number, and implement input and output in the main program
Use C++ to write a function to determine whether a number is a prime number, and implement input and output in the main program

First define the integer variable n, input n, judge it through the judgment function, and finally output it.

For example: //The reference code is as follows:

#include?"iostream"

#include?"stdio.h"?

using?namespace?std;?

int?fun(int?n){//Judgement function, returns 1 if it is a prime number, otherwise returns 0?

int?ans?= 1;

for(int?i=2;i

if(n%i= =0){

ans=0;

break;

}

return?ans; ?

}

int?main()

{

int?n;

cin>>n;//Enter n?

if(fun(n))//Judge output?

cout<

else

< p> ? cout<

return?0;?

}

/*

Run result:

29

29 is a prime number

*/