Program to find the prime numbers below a given number.

Thursday, December 4th, 2008

The program is to find all the prime numbers in the range, which starts from zero and ends at user defined limit. A prime number is a one, whose divisors are 1 and the number itself.

Logic: This is a slightly modified version of the previous program. Here, user need to enter the number as the upper limit for the for the iteration loop to find the prime number The outer for loop traces the iteration till the limit,, wherein each of iteration inner for loop checks the present number is prime or not, with the previous program’s logic. If it is, it prints out the present number.

In this program, the lower limit is constant zero. We can change that also, to make the program fully flexible, and to print out all the perfect number between the given range.

Program to find the prime numbers below a given number.

#include<stdio.h>
void main()
{
int i, prime, lim_up, n;
clrscr();
printf(“nnt ENTER THE UPPER LIMIT…: “);
scanf(“%d”, &lim_up);
printf(“nnt PRIME NUMBERS ARE…: “);
for(n=1; n<lim_up; n++)
{
prime = 1;
for(i=2; i<n; i++)
if(n%i == 0)
{
prime = 0;
break;
}
if(prime)
printf(“nnttt%d”,n);
}
getch();
}
Download exe and source code here.

Avatar Image

Author Name :
Ranjith

Total : 3 Comments


3 Responses to “Program to find the prime numbers below a given number.”

  1. Nazrul islam says:

    It’s may be easier then give up.

    #include
    #include
    void main()
    {
    clrscr();
    int i,n,count=0;
    printf(“\n Enter the value:”);
    scanf(“%d”,&n);
    for(i=0;i<=n;i++)
    {
    if(n%i==0)
    count ++;
    }
    if(count<=2)
    printf(“This is Prime Value”);
    else
    printf(“This is not prime Value”);
    getch();
    }

  2. Nazrul islam says:

    This program for only enter value.

  3. nagesh says:

    nice logic used

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup