codescracker


c

C Program to Print Prime Numbers



« Previous Program Next Program »


Print Prime Numbers in C

To print all prime number between the particular range in C programming, you have to ask to the user to enter the range (i.e., ask to enter the starting and ending number ), and check division from 2 to one less that that number, if the number is divided by any number from 2 to on less than (n-1) that number then that number will not be prime otherwise that will be prime number

C Programming Code to Print Prime Numbers

To print prime number between range for example : to print prime number between 3 to 29, here 3 is the starting number and 29 is the ending number, so following C program ask to the user to enter the starting and ending number to find and print all the prime numbers exists between the given range (here we are including the starting and ending number also if they are prime):

/* C Program - Print Prime Numbers */
		
#include<stdio.h>
#include<conio.h>
void main()
{
	clrscr();
	int start, end, i, j, count=0;
	// to print all prime number between any range
	// enter the two number (starting and ending)
	printf("Enter starting number : ");
	scanf("%d",&start);
	printf("Enter ending number : ");
	scanf("%d",&end);
	printf("Prime Number Between %d and %d is :\n", start, end);
	for(i=start; i<=end; i++)
	{
		count=0;
		for(j=2; j<i; j++)
		{
			if(i%j==0)
			{
				count++;
				break;
			}
		}
		if(count==0)
		{
			printf("%d  ",i);
		}
	}
	getch();
}

When the above C Program is compile and executed, it will produce the following result:

c program print prime numbers

Same Program in Other Programming Language

You may also like same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test