Pages

Ads 468x60px

Saturday, 2 June 2012

C program for PRIME NUMBER FINDING


C program for PRIME NUMBER FINDING



The program is to find all the prime numbers falls inside the user defined range.
 A prime number is a one, whose divisors are 1 and the number itself. Logic: This
 is advanced version of the previous program. Here, user need to enter two numbers
 as the lower and upper limits for the iteration loop to find the prime number in
 between. 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 prime 
number’s logic. If it is, it prints out the present number. In this program, both the
 lower limit and the upper limit are variables, and so is flexible. These three programs
 show, how we can upgrade the logic to make the code flexible one.





CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,c=0,j,a,b;
clrscr();
printf("Enter any number:");
scanf("%d%d",&a,&b);
for(i=a;i<=b;i++)
{
c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
{
c++;
//printf("i=%d and j=%d and c=%d\n",i,j,c);
}
}
if(c==2)
{
printf("%d\n",i);
}
}
getch();
}

0 comments:

Post a Comment

Total Pageviews