C Program to Print Prime Numbers From 1 to 100

In this post, we will learn how to print prime numbers from 1 to 100 using the C Programming language.

In the previous post, you have seen how to check whether a number is prime or not. Today, we will print all the prime numbers lying between 1 to 100 using the following approaches:

  1. Using For Loop
  2. Using While Loop

So, without further ado, let’s begin this tutorial.

C Program to Print Prime Numbers From 1 to 100

// C Program to Print Prime Numbers From 1 to 100
#include <stdio.h>
int main(){
    int i, num, count;
    
    // Checking for prime numbers
    for (num = 1; num <= 100; num++){
        count = 0;
        for (i = 2; i <= num/2; i++){
            if (num % i == 0){
                count++;
                break;
            }
        }
        
        // Checking and Printing Prime Numbers
        if (count == 0 && num != 1){
            printf("%d \n", num);
        }
    }
    return 0;
}

Output

2 
3 
5 
7 
11 
13 
17 
19 
23 
29 
31 
37 
41 
43 
47 
53 
59 
61 
67 
71 
73 
79 
83 
89 
97 

How Does This Program Work ?

    int i, num, count;

In this program, we have declared three integer data type variables named i, num, and count.

    for (num = 1; num <= 100; num++){
        count = 0;
        for (i = 2; i <= num/2; i++){
            if (num % i == 0){
                count++;
                break;
            }
        }

For every number, we check its factor. If that iteration of the for loop contains a factor, then count values increases.

        // Checking and Printing Prime Numbers
        if (count == 0 && num != 1){
            printf("%d \n", num);
        }

Now, all the numbers which don’t have a factor are prime numbers. We print all those numbers using printf() function.

C Program to Print Prime Numbers From 1 to 100 Using While Loop

// C Program to Print Prime Numbers From 1 to 100 Using While Loop
#include <stdio.h>
int main(){
    int i, num = 1, count;
    
    // Checking for the factors
    while (num <= 100){
        count = 0;
        i = 2;
        while (i <= num/2){
            if (num % i == 0){
                count++;
                break;
            }
            i++;
        }
        
        // Printing prime numbers
        if (count == 0 && num != 1){
            printf("%d \n", num);
        }
        num++;
    }
    return 0;
}

Output 

2 
3 
5 
7 
11 
13 
17 
19 
23 
29 
31 
37 
41 
43 
47 
53 
59 
61 
67 
71 
73 
79 
83 
89 
97

Conclusion

I hope after going through this post, you understand how to print prime numbers from 1 to 100 using the C Programming language.

If you have any doubt regarding the program, then contact us in the comment section. We will be delighted to assist you.

Also Read:

1 thought on “C Program to Print Prime Numbers From 1 to 100”

  1. // C Program to Print Prime Numbers From 1 to 100
    #include
    int main(){
    int i, num, count;

    // Checking for prime numbers
    for (num = 1; num <= 100; num++){
    count = 0;
    for (i = 2; i <= num/2; i++){
    if (num % i == 0){
    count++;
    break;
    }
    }

    // Checking and Printing Prime Numbers
    if (count == 0 && num != 1){
    printf("%d \n", num);
    }
    }
    return 0;
    }

    ***I don't understand, if I put 0 at the start of count variable (ie, int i,num,count=0 like this) the code is not working. But if you put "count=0" inside the for loop it is working. Why is this happening?

Leave a Comment

Your email address will not be published. Required fields are marked *