Finding Prime Numbers Within a Given Range


This program is a basic implementation of finding prime numbers within a given range. The program prompts the user to enter a starting and ending number, then uses two while loops to check if each number within the range is prime. The first while loop iterates through all numbers within the given range, and the second while loop iterates through all numbers less than the current number in the first loop. Within the second loop, the program checks if the current number in the first loop is divisible by the current number in the second loop. If it is, the variable prime is incremented.

After the second loop completes, the program checks if the prime variable is equal to zero. If it is, the current number in the first loop is a prime number, and it is printed to the console. This process continues for all numbers within the range, and at the end of the program, all prime numbers within the given range are displayed.

Source Code

#include<stdio.h>
int main()
{
  int i,n=2,a,b,prime=0;
  printf("\nEnter the starting number:");
  scanf("%d",&i);
  printf("\nEnter the ending number:");
  scanf("%d",&b);
  while(i<=b)
  {
    prime=0;
    n=2;
    while(n<i)
    {
      if(i%n==0)
      {
        prime++;
      }
      n++;
    }
    if(prime==0)
    {
      printf("\n%d",i);
    }
    i++;
  }
  return 0;
}
To download raw file Click Here

Output


Enter the starting number:1
Enter the ending number:15
1
2
3
5
7
11
13


List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C