Search This Blog

Program to List All Prime Numbers Less Than Given Number

This is a c program to list all prime numbers less than a given number. Within a loop it checks each number less than the given number, whether it is prime or not.


#include<stdio.h>
#include<math.h>

int isPrime(int n)
{
int root,i;
root=sqrt(n);
for(i=2;i<=root;i++)
    {
    if(n%i==0)
        return 0;
    }
return 1;
}

void main()
{
int i,input;
printf("\nEnter a number\n");
scanf("%d",&input);
printf("\nThe following are the prime numbers less than %d:\n",input);
for(i=2;i<input;i++)
    {
    if(isPrime(i))
        printf("%d, ",i);
    }
}

No comments: