18. Comprehensive application to determine prime numbers

/*
Enter an integer greater than 1 and determine whether it is a prime number or a composite number.

Prime Number: A natural number that can only be integers by 1 and itself.
Natural numbers: integers from 0 to infinity.

Analysis:
In fact, any natural number can be divisible by 1 and itself (except 0),
so, as long as it can be divisible by one of the other (2 to n-1) numbers, this
natural number is not a prime number, but a composite number.
*/
#include "stdio.h"
void main()
{
int n;
int i;
int x;
int qizi;

x=0;
printf("Please enter an integer greater than 0:");
nn:x=scanf( "%d",&n);
if(x!=1||n<1)
{
printf("The data input is incorrect, please re-enter:");
fflush(stdin);
goto nn;
}

if(n!= 1)
{
qizi=1;
for(i=2;i<=n-1;i++)//Numbers other than 1 and itself
{
if(n%i==0)//Determine whether the input number can be other integers
{
qizi=0;
break;
}
}

if(qizi==0)//After the loop ends, do you judge whether the flag is down?
printf("%d is composite\n",n);
else
printf("%d is prime\n",n);
}
else
printf("%d is neither prime nor composite\n",n) ;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325792787&siteId=291194637