C Language judge whether prime code _c language

Source: Internet
Author: User
Tags stdin

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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.