Copy Code code as follows:
#include <stdio.h>
BOOL Isprimenum (int x)
{
if (x = = 1)
return false;
else if (x <= 0)
return false;
else if (x = = 2)
return true;
Else
{
for (int i = 2; i < x; i++)
{
if (x%i = 0)
return false;
}
return true;
}
}
int main (void)
{
int x;
Char ch;
do{
printf ("Please enter a natural number greater than 1: \ n");
scanf ("%d", &x);
if (isprimenum (x) = = False)
printf ("%d is not prime \ n", x);
else if (isprimenum (x) = = True)
printf ("%d is prime \ n", x);
printf ("Continue (y/n): \ n");
scanf ("%c", &ch);
}while (' y ' = ch | | ' Y ' = ch);
return 0;
}
However, when running, there is a problem.
Originally write Do...while is to save trouble, repeat judgment. However, it is not leng effect.
Later, it was modified:
Copy Code code as follows:
#include <stdio.h>
BOOL Isprimenum (int x)
{
if (x = = 1)
return false;
else if (x <= 0)
return false;
else if (x = = 2)
return true;
Else
{
for (int i = 2; i < x; i++)
{
if (x%i = 0)
return false;
}
return true;
}
}
int main (void)
{
int x;
Char ch;
do{
printf ("Please enter a natural number greater than 1: \ n");
scanf ("%d", &x);
if (isprimenum (x) = = False)
printf ("%d is not prime \ n", x);
else if (isprimenum (x) = = True)
printf ("%d is prime \ n", x);
printf ("Continue (y/n): \ n");
scanf ("%c", &ch)//Add a space here
}while (' y ' ==ch | | ' Y ' = ch);
return 0;
}
In this way, the problem is magically solved.
And then, again, this change:
Copy Code code as follows:
#include <stdio.h>
BOOL Isprimenum (int x)
{
if (x = = 1)
return false;
else if (x <= 0)
return false;
else if (x = = 2)
return true;
Else
{
for (int i = 2; i < x; i++)
{
if (x%i = 0)
return false;
}
return true;
}
}
int main (void)
{
int x;
Char ch;
do{
printf ("Please enter a natural number greater than 1: \ n");
scanf ("%d", &x);
if (isprimenum (x) = = False)
printf ("%d is not prime \ n", x);
else if (isprimenum (x) = = True)
printf ("%d is prime \ n", x);
printf ("Continue (y/n): \ n");
scanf ("\n%c", &ch)//Add a newline character ' \ n ' here.
}while (' y ' ==ch | | ' Y ' = ch);
return 0;
}
It's no problem.
So, to sum up, the problem appears in the input number when we press ENTER, ' \ n ' is also saved in the input stream
So, look at the first code:
Copy Code code as follows:
int main (void)
{
int x;
Char ch;
do{
printf ("Please enter a natural number greater than 1: \ n");
scanf ("%d", &x);
if (isprimenum (x) = = False)
printf ("%d is not prime \ n", x);
else if (isprimenum (x) = = True)
printf ("%d is prime \ n", x);
printf ("Continue (y/n): \ n");
scanf ("%c", &ch); Because ' \ n ' is still in the input stream, so it becomes ch = ' \ n ';
}while (' y ' ==ch | | ' Y ' = ch); ' Y '!= ' && ' y '!= ' \ n ';
return 0; So the program is return.
}
Alternatively, you can use Fflush (stdin) to empty the input buffer.
Copy Code code as follows:
#include <stdio.h>
BOOL Isprimenum (int x)
{
if (x = = 1)
return false;
else if (x <= 0)
return false;
else if (x = = 2)
return true;
Else
{
for (int i = 2; i < x; i++)
{
if (x%i = 0)
return false;
}
return true;
}
}
int main (void)
{
int x;
Char ch;
do{
printf ("Please enter a natural number greater than 1: \ n");
scanf ("%d", &x);
if (isprimenum (x) = = False)
printf ("%d is not prime \ n", x);
else if (isprimenum (x) = = True)
printf ("%d is prime \ n", x);
printf ("Continue (y/n): \ n");
Fflush (stdin); Empty the input buffer
scanf ("%c", &ch);
}while (' y ' ==ch | | ' Y ' = ch);
return 0;
}