16 September 2014

C Program to find the Largest Prime number less than the given number.

Given a number (as command line argument), find the Largest Prime number less than the given number. For e.g., given 8, the program should return 7.


#include <stdio.h>
int main()
{
    int i,n=2,m,j=0;
    int a[100]={'\0'};
    printf("enter the number in the range[3-500]\n");
    scanf("%d",&m);
    while(n<m)
    {
        for(i=2;i<n;i++)
        {
            if(n%i==0)
            goto t;
        }
        a[j++]=n;
        t : n++;
    }
    printf("largest prime number less than given number is %d\n",a[--j]);
}

Post a Comment