Example of using C language to judge 100 prime numbers (C language for prime) _c language

Source: Internet
Author: User

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

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.