C Program to Display Prime Numbers Between Two numbers

 

C Program to Display Prime Numbers Between Two numbers

Prime numbers between two numbers we will also learn how to display prime number between 1 to n numbers. Now first of all we learn about prime numbers.

  • Write a C program to print all prime numbers between 1 to N using for loop.
  • Wap in C to print prime numbers between 1 to 100.

Required Knowledge

Define prime number

A Prime number is a natural number greater than 1 that is only divisible by either 1 or itself. All numbers other than prime numbers are known as composite numbers. There are infinitely many prime numbers, here is the list of first few prime numbers
2 3 5 7 11 13 17 19 23 29 31 37….

step 1. in step 1 processors are given to include header file for prime numbers
step 2. we used main function
step 3.  we have declared variables
step4. two numbers are inputed with the help of printf and scanf fuction
step5. in this step external loop is given here we for loop which increase their value from 1 to next and so on

Algorithm for prime number

we just write algorithm for 1 to n number you can change it according to your need and requirements
alogorighm
Algorithm to check whether a number is prime number or not
Let, N be a positive number.

  • For every number i, between 2 to N/2(2<= i <= N/2) check whether i divides N completely(check If i is a factor of N).
  • if (N % i == 0), then N cannot be a Prime number.
  • If none of the number between 2 to N/2 divides N completely then N is a prime number.

Example of Program

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
long i,prime,j,n1,n2;
printf("Enter start and end numbers for prime");
scanf("%ld %ld",&n1,&n2);
for(i=n1;i<=n2;i++)
{
 prime=1;
 for(j=2;j<i;j++)
 {
 if(i%j==0)
 {
 prime=0;
 break;
 }
 }
 if(prime==1)
 printf("%ld\t",i);
}
getch();
}

Output

Enter two numbers(intervals): 20
50
Prime numbers between 20 and 50 are: 23 29 31 37 41 43 47

Hits: 2

Leave a Reply

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