Monday, August 13, 2012

Write a program to find Nth prime number in a given range, inclusive of the first and the last element in that range

#include<stdio.h>
main()
{
 long int i=0,j=0;
 long int n1,n2,t=0,v;
 int k;
 int flag=0;
 printf("\n FROM the number n1 To the number n2 enter the nth prime no you want ");
 scanf("%ld %ld %ld",&n1,&n2,&v);
 if(v>0&&n2>n1&&n1<1500000&&n2<1500000)
 {
 for(i=n1;i<=n2;i++)
 {
  for(j=1,k=0;j<=i;j++)
  {
   if(i%j==0)
   k++;
  }
 if(k==2)
 {
 t++;
 if(t==v)
 {
 printf("\n %ld",i);
 flag=1;
 break;
 }
 }
 }
 if(flag==0)
 {
 printf("\n no prime number is present at this index");
 }
 }
 else
 {
 printf("Invalid Input");
 }
 getch();
 }


No comments:

Post a Comment

DC motor control with Pulse Width Modulation Part 1

DC Motor intro DC motor is a device which converts electrical energy into kinetic energy. It converts the DC power into movement. The typica...