Enter a number from the console to determine whether the number is prime (prime).
Copy Code code as follows:
#include <stdio.h>
/*
* Determine the prime number within 100
*/
Define a function to determine whether it is a prime
int isprime (int num) {
int i;
Loop from 2 until the square of I is less than or equal to the given number.
for (i = 2; i*i <= num; i++) {
if ((num% i) = = 0) {
return 0;
}
}
}
int main (int argc, const char *argv[])
{
int re;
int input;
printf ("Please enter an integer within 100:");
scanf ("%d", &input);
if (Input > 1) {
Re = IsPrime (input);
if (re = = 0) {
printf ("%d is not prime \ n", input);
}else{
printf ("%d is a prime number \ n", input);
}
}else{
if (input = = 1) {
printf ("1" is neither a prime number nor a composite number.) \ n ");
}else{
printf ("Please enter a positive integer ^.^\n greater than or equal to 1");
}
}
return 0;
}