C Program to Print Prime Numbers from 1 to 100

How to write a C Program to Print Prime Numbers from 1 to 100 or Minimum to maximum. Or, prime Numbers within a specific range, and some of the prime numbers within a range using For Loop and While Loop.

C Program to Print Prime Numbers from 1 to 100 Using For Loop

In this program to print Prime Numbers between 1 to 100, the first For Loop will make sure that the number is between 1 and 100 in C.

/* C Program to Print Prime Numbers between 1 to 100 using For Loop */
 
#include <stdio.h>
 
int main()
{
  int i, Number, count; 
  
  printf(" Prime Number from 1 to 100 are: \n"); 
  for(Number = 1; Number <= 100; Number++)
  {
    count = 0;
    for (i = 2; i <= Number/2; i++)
    {
  	if(Number%i == 0)
  	{
     	  count++;
  	  break;
	}
    }
    if(count == 0 && Number != 1 )
    {
	printf(" %d ", Number);
    }  
  }
  return 0;
}
 Prime Number from 1 to 100 are: 
 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

TIP: We already explained the logic to check whether the given is prime or not in C Program to Find Prime Number article in C Programming.

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

In this C program to return prime numbers from 1 to 100, we just replaced the For Loop in the above example with a While loop. If you don’t understand the While Loop, please refer While Loop article here: WHILE LOOP

/* C Program to Print Prime Numbers from 1 to 100 using While Loop */
 
#include <stdio.h>
 
int main()
{
  int i, Number = 1, count; 
  
  printf(" Prime Number from 1 to 100 are: \n"); 
  while(Number <= 100)
  {
    count = 0;
    i = 2;
    while(i <= Number/2)
    {
      if(Number%i == 0)
      {
        count++;
	break;
      }
      i++;	
    }	
    if(count == 0 && Number != 1 )
    {
	printf(" %d ", Number);
    }
    Number++; 
  }
  return 0;
}
 Prime Number from 1 to 100 are: 
 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

C Program to Print Prime Numbers from 1 to N Using For Loop

Instead of printing prime numbers from 1 to 100, you can allow the user to decide the minimum and maximum values.

This program allows the user to enter Minimum and Maximum values — next, this C program prints prime numbers between Minimum and Maximum values using For Loop.

/* C Program to Print Prime Numbers from 1 to n using For Loop */
 
#include <stdio.h>
 
int main()
{
  int i, Number, count, Minimum, Maximum; 

  printf("\n Please Enter the Minimum & Maximum Values\n");
  scanf("%d %d", &Minimum, &Maximum);
 
  printf("Prime Numbers Between %d and %d are:\n", Minimum, Maximum);  
  for(Number = Minimum; Number <= Maximum; Number++)
  {
    count = 0;
    for (i = 2; i <= Number/2; i++)
    {
      if(Number%i == 0)
      {
  	count++;
  	break;
      }
    }
    if(count == 0 && Number != 1 )
    {
       printf(" %d ", Number);
    }  
  }
  return 0;
}
 Please Enter the Minimum & Maximum Values
20
150
Prime Numbers Between 20 and 150 are:
 23  29  31  37  41  43  47  53  59  61  67  71  73  79  83  89  97  101  103  107  109  113  127  131  137  139  149

C Program to Print Prime Numbers from 1 to N Using While Loop

This program to print prime numbers from 1 to 100 is the same as above, but we replaced the For loop with While Loop.

/* C Program to Print Prime Numbers from 1 to N using While Loop */
 
#include <stdio.h>
 
int main()
{
  int i, Number, count, Minimum, Maximum; 
  
  printf("\n Please Enter the Minimum & Maximum Values\n");
  scanf("%d %d", &Minimum, &Maximum);
  Number = Minimum; 
  
  printf("Prime Numbers Between %d and %d are:\n", Minimum, Maximum);    
  while(Number <= Maximum)
  {
    count = 0;
    i = 2;
    while(i <= Number/2)
    {
      if(Number%i == 0)
      {
        count++;
	break;
      }
      i++;	
    }	
    if(count == 0 && Number != 1 )
    {
       printf(" %d ", Number);
    }
    Number++; 
  }
  return 0;
}
C Program to Print Prime Numbers from 1 to 100 4

C Program to calculate Sum of Prime Numbers from 1 to 100

This program finds prime numbers from 1 and 100. Next, it is going to add those numbers to find the sum of all prime numbers between 1 and 100 in C.

/* C Program to find Sum of Prime Numbers Between 1 to 100 */
 
#include <stdio.h>
 
int main()
{
  int i, Number, count, Sum = 0; 
  
  for(Number = 1; Number <= 100; Number++)
  {
    count = 0;
    for (i = 2; i <= Number/2; i++)
    {
  	if(Number%i == 0)
  	{
  	   count++;
  	   break;
	}
    }
    if(count == 0 && Number != 1 )
    {
	Sum = Sum + Number;
    }  
  }
  printf(" Sum of Prime Numbers between 1 to 100 =  %d", Sum); 
  return 0;
}
 Sum of Prime Numbers between 1 to 100 =  1060

C Program to find Sum of Prime Numbers from 1 to N

Instead of adding prime numbers from 1 to 100, you can allow the user to decide the minimum and maximum values. This program allows the user to enter Minimum and Maximum values. Next, this C program finds the sum of prime numbers between Minimum and Maximum values using For Loop.

/* C Program to Print Sum of Prime Numbers from 1 to 100 */
 
#include <stdio.h>
 
int main()
{
  int i, Number, count, Sum = 0, Minimum, Maximum; 

  printf("\n Please Enter the Minimum & Maximum Values\n");
  scanf("%d %d", &Minimum, &Maximum);
  
  for(Number = Minimum; Number <= Maximum; Number++)
  {
    count = 0;
    for (i = 2; i <= Number/2; i++)
    {
  	if(Number%i == 0)
  	{
     	  count++;
  	  break;
	}
    }
    if(count == 0 && Number != 1 )
    {
       Sum = Sum + Number;
    }  
  }
  printf(" Sum of Prime Numbers between %d and %d =  %d", Minimum, Maximum, Sum); 
  return 0;
}
 Please Enter the Minimum & Maximum Values
100
200
 Sum of Prime Numbers between 100 and 200 =  3167

Comments are closed.