Tuesday, 28 January 2014

c program for prime till given number

c program for prime till given number. This c program find and count the Prime number upto the given number, you desire . While c program to find prime number using recursion and not using recursion can be made easily, this programs needs some tricks to compute prime number. We need jump statement i.e goto. Program uses goto Statement which is very infrequently used but sometime desired at some places and so as here. So in this program will we also learn to use goto statement. Below is the program with its output, with prime number upto 100.

c program for prime till given number

#include<stdio.h>
#include<conio.h>

int main()
{
    int n=2, count=0;                  //count variable store the number of prime number
    int i, m;
    printf("enter the number upto which you want to find prime numbers : ");
    scanf("%d",&m);
    printf("prime numbers are : ");
    while(n<m)
    {
        for(i=2; i<n; i++)
        {
        if(n%i==0)
        goto t;                             // goto is referring to line or code 't'
        }
    printf("\t%d ", n);
    count++;
    t : n++;                                    //when goto is executed it will directly jump to this statement
    }
    printf("\n\nNumber of prime numbers are %d",count);
    getch();
}

output of c program for prime till given number

c program for prime till given number

No comments:

Post a Comment