Solution of Prime number (C language)

Source: Internet
Author: User

    1. Enter all the primes between 100 and 200.

(1) through a two-tier for loop

#include <stdio.h>

int main ()

{

int i;

Int J;



for (i=100;i<=200;i++)

{

for (j=2;j<=i-1;j++)

{

if (i%j==0)

Break

}

if (j==i)

printf ("%d", I);

}

return 0;

}

Optimization:

#include <stdio.h>

int main ()

{

int i;

Int J;

for (i=101;i<=200;i=i+2)

{

for (j=2;j<=i/2;j++)

{

if (i%j==0)

Break

}

if (J>=I/2)

printf ("%d", I);

}

return 0;

}

(2) by comparing the number of records

#include <stdio.h>

int main ()

{

int i,j;

int count=0;

for (i=100;i<=200;i++)

{

count=0; Each time count needs to start at 0

for (j=2;j<=i-1;j++)

{

if (i%j==0)

Break

Else

count++;

}

if (count==i-2)//Record Count comparison

printf ("%d", I);

}

return 0;

}

(3) Boolean type

#include <stdio.h>

int main ()

{

int i,j;

BOOL Flag;

for (i=100;i<=200;i++)

{

Flag = true;

for (j=2;j<=i-1;j++)

{

if (i%j==0)

{

Flag=false;

Break

}

}

if (flag)

printf ("%d", I);

}

return 0;

}

Note: Applying a Boolean type is not possible in VC6.0. Can be implemented in Microsoft Visual Studio 2008.


Solution of Prime number (C language)

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.