Type Here to Get Search Results !

C PROGRAM TO PRINT NTH PRIME NUMBER

1



C PROGRAM TO PRINT NTH PRIME NUMBER 

If we give 1 as input 

we can get 1st prime number(2) as out put.

 If we give input as 2.

 we can get second prime number(3) as output and  input upto n.

Sample input.

1

Sample output.

2   /*1st prime number*/


Sample input.

2

Sample output.

3   /*2nd prime number*/


PROGRAM-

#include<stdio.h>
int main()
{
int i,j,n,t;
printf("Enter n\n");
scanf("%d",&n);
int count =0;
for(i=2;i<=32767;i++)
{
int c=0;
for(j=1;j<=i;j++)
  {
      if(i%j==0)
      {
          c++;
       }
  }
       if(c==2)
       {
           count++;
         
       }
        if(count==n)
        break;
  }
printf("%d",i);
}

CLICK HERE TO RUN






Friends in this program on 8th line (for loop) i took  i<=37000; . You can take any value from 2 and upto the integer range(32762)


In this program we have to check the condition of prime number . 
We know that a number is said to be a prime number when it has only two factors that are 1 and itself( ex- 5 has factors 1 and 5). 
 so we use nested loops to check all numbers from 2 to n*n .       
In first loop we take numbers from 2 (becuase 1st prime number is 2) and increment the value upto n. 
And in second loop we take numbers from 1 to i. Here we check the factors of i. If i%j==0 we increase the count value(initially we take count=0) . And if count ==2 means the number have two factors.
then we write a condition that if the given number has two factors we increment the value of c (initially c value is0). if c value is equal to the n then we print the value of i .that is the nth prime number.


THANK YOU 

        COPYRIGHTⒸ TO MOHANKADALI

Post a Comment

1 Comments
  1. https://leetcode.com/problems/plates-between-candles/description/

    ReplyDelete
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Below Post Ad