top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Write a program to find nth prime number using Java?

+2 votes
4,700 views
Write a program to find nth prime number using Java?
posted Apr 22, 2017 by Azhar Keer

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+3 votes
 
Best answer

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++){
if (num % i == 0) {
break;
}
}
if ( i == num){
count = count+1;
}
}
System.out.println("Value of nth prime: " + num); } }

answer Apr 28, 2017 by Naziya Raza Khan
Let me execute this problem
i need this c language
Similar Questions
+2 votes

Rohit wants to add the last digits of two given numbers.
For example, If the given numbers are 267 and 154, the output should be 11.

Below is the explanation -
Last digit of the 267 is 7
Last digit of the 154 is 4
Sum of 7 and 4 = 11

Write a program to help Rohit achieve this for any given two numbers.

Note: The sign of the input numbers should be ignored. i.e.
if the input numbers are 267 and 154, the sum of last two digits should be 11
if the input numbers are 267 and -154, the sum of last two digits should be 11
if the input numbers are -267 and 154, the sum of last two digits should be 11
if the input numbers are -267 and -154, the sum of last two digits should be 11

+5 votes

Example :
Let the list be {2,3,5} and Assume always 1 be included then

2th number is 2
3th number is 3
4th number is 4
5th number is 5
6th number is 6
7th number is 8
8th number is 9
9th number is 10
10th number is 12
11th number is 15 and so on...

0 votes

Write a program which finds all pairs of elements in an array whose sum is equal to a given number

Input  array: [-2,1,3,5,6,7,8,10,12,15,17,19,20]
Sum : 20

Output

6, 12
3, 15
1, 17
-2, 20
8, 10

Required best time complexity and effective code

...