Can someone help me with the code to find the nth prime number in Java? I am practicing for a coding competition and was stuck with this! I tried few things but those exceeded the time limit. So can someone help me with the problem? Mar 4 in Java 4,072 views

## 1 answer to this question.

Here is the code to find nth prime number.

```import java.util.Scanner;
public class NthPrime {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter n to compute the nth prime number: ");
int nth = sc.nextInt();
int num, count, i;
num=1;
count=0;

while (count < nth){
num=num+1;
for (i = 2; i <= num; i++){ //Here we will loop from 2 to num
if (num % i == 0) {
break;
}
}
if ( i == num){//if it is a prime number
count = count+1;
}
}
System.out.println("Value of nth prime: " + num);
}
}```

Output:

```Enter n to compute the nth prime number: 5
Value of nth prime: 11``` answered Mar 4 by
• 56,160 points

## In Java, how to find out the min/max values from array of primitive data types?

import java.util.Arrays; import java.util.Collections; import org.apache.commons.lang.ArrayUtils; public class MinMaxValue { ...READ MORE

## How to round any number to n decimal places in Java?

You can use setRoundingMode() method to get ...READ MORE

## How to print java array in the simplest way?

String[] arr = new String[] {"John", "Mary", ...READ MORE

## Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

## Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

+1 vote

## What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE