Easy Tutorial
❮ C Exercise Example81 C Exercise Example59 ❯

C Exercise Example 12

C Language Classic 100 Examples

Title: Determine the prime numbers between 101 and 200.

Program Analysis: The method to determine prime numbers: Use a number to divide 2 to sqrt(this number). If it can be divided evenly, this number is not a prime; otherwise, it is a prime.

Example

//  Created by www.tutorialpro.org on 15/11/9.
//  Copyright © 2015 tutorialpro.org. All rights reserved.
//

#include <stdio.h>

int main()
{
    int i, j;
    int count = 0;

    for (i = 101; i <= 200; i++)
    {
        for (j = 2; j < i; j++)
        {
            // If j can be evenly divided by i, break the loop
            if (i % j == 0)
                break;
        }
        // Check if the loop was exited prematurely, if j < i, it means there is a number between 2~j that i can be divided by
        if (j >= i)
        {
            count++;
            printf("%d ", i);
            // New line, use count to count, every five numbers, new line
            if (count % 5 == 0)
                printf("\n");
        }
    }
    return 0;
}

The above example output is:

101 103 107 109 113
127 131 137 139 149
151 157 163 167 173
179 181 191 193 197
199

C Language Classic 100 Examples

❮ C Exercise Example81 C Exercise Example59 ❯