C programming is a general purpose widely used high level programming language. Almost all types of computer programs are written in this language. It is the basic programming language. Write a C program to find Prime number between two numbers inputted by user. We will also learn how to display prime number between 1 to n numbers. Now first we learn about prime numbers.
- Write a C program to print all prime numbers between 1 to N using for loop.
- Write in C to print prime numbers between 1 and 100.
Required Knowledge
- C print f and scan-f functions
- For loop in C
Define Prime Number in C Programmings
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
Step 4
. Two numbers are inputted with the help of print f and scan f fiction.
Step 5
. 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 no
we just write algorithm for 1 to n number you can change it according to your need and requirements
Algorithm.
Algorithm to check whether a number is prime or not
Let, N be a positive number.
- For every number i, between 2 to N/2(2<= i <= N/2) check whether I divide 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