Solved programming problem

Find Nth Prime Number Using Function in C.

Source code

#include <stdio.h>

int GenNthPrime(int n)

{

    int i,j,count=0,flag=0;

    for(i=2; i>0; i++)

    {

        flag=0;

        for(j=2; j<i; j++)

        {

            if(i%j==0)

            {

                flag=1;

                break;

            }

        }

        if(flag==0)

        {

            count++;

        }

        if(count==n)

        {

            printf("
  %dth Prime Number is: %d
",n,i);

            break;

        }

    }

}

int main()

{

    int n;

    printf("
Please Enter a number:");

    scanf("%d",&n);

    GenNthPrime(n);///call the function

   

    return 0;

}

Output


Please Enter a number:5

  5th Prime Number is: 11

 

Join the discussion

Add your comment