Search

Sunday, March 3, 2019

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

Posted By Manisha Gupta
n this C program to print prime numbers from 1 to 100, we just replaced the For loop in the above example with 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;
}
OUTPUT
C Program to Print Prime Numbers from 1 to 100 2

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

Instead of printing prime numbers from 1 to 100 in C, 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 print 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;
}
OUTPUT
C Program to Print Prime Numbers from 1 to 100 3

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

This C program to print prime numbers from 1 to 100 is 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;
}
OUTPUT
C Program to Print Prime Numbers from 1 to 100 4

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

This C program find prime numbers from 1 and 100. Next, it is going to add those number to find the sum of all prime numbers between 1 and 100.
/* 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;
}
OUTPUT
C Program to Print Prime Numbers from 1 to 100 5

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 program find 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;
}
OUTPUT
C Program to Print Prime Numbers from 1 to 100 6
Thank you for Visiting Our Blog

Follow on Facebook

ManishaTech . 2017 Copyright. All rights reserved. Designed by Manisha Gupta | Manisha Gupta