C Program to find Prime Numbers Between 1 to 100

      This C program finds all the prime numbers between 1 to 100. The same logic which was used to find whether given number is prime or not is used in the inner for loop, while the outer for loop iterates from 1 through 100 to give each natural number as input to inner for loop.

C Program to find Prime Numbers Between 1 to 100



Source Code :

#include<stdio.h>
#include<math.h>
int main()
{
  int i, j,r;
  for(i = 2; i <= 100; ++i)                         // Step 1
   {
    r=1;
    for(j = 2; j <= sqrt(i); ++j)                   // Step 2 
    {
     r=i%j;
     if(r == 0)                                     
     break;
    }
    if(r!=0)                                        // Step 3
    printf("%d\n", i);
   }
 return 0;
}


edit-code-image

Sample Test cases :

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

Explanation:

Step 1: The outer for loop iterates from 1 through 100 giving each natural number as input to inner for loop.

Step 2: The inner for loop determines whether given number is prime or not using remainder concept ( refer prime or not program for better clarification).

Step 3: If the remainder is non-zero then it implies that particular number is prime number and that number is printed to output screen until outer for loop reaches to 100.

More Insights:

1. Prime Number

More Content : Explore More