N-th Prime Number


Example problem:
What is 6th prime number?
Answer:
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6-th prime is 13

Now, what is 10001-th prime number?

It is simple, if we use program to count it:

/*
  Name: N-th Prime Number
  Copyright: 2009
  Author: Windu Purnomo
  Date: 23/11/09 11:40
  Description: Get n-th prime number, after list it.
*/

#include<stdio.h>
main(){
    int i, j, n;
    int flag = 1;
    int counter = 0;
    
    scanf("%d", &n);
    for(i=2; i>0; i++){
       for(j=2; j<i; j++){
          if(i%j==0){
             flag = 0;
             break;
          }
       }
       if(flag){
          counter++;
          if(counter == n){
             printf("It is %d-th prime number: %d", n, i);
             break;
          }
       }else{
          flag = 1;
       }
    }
    getch();
    return 0;
}

About windupurnomo

I'm interested in programming. I am active with several programming such as Java, C #, C, JavaScript, HTML. I'm also develop desktop application (Java Swing), Mobile Application (Android), and Web programming (ASP MVC).
This entry was posted in C Language, programming and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s