
java program for prime nos from 1 to n

Java Find Prime Numbers
import java.util.*;
class PrimeNumbers{
public static void main (String args[]){
Scanner input=new Scanner(System.in);
System.out.print("Enter value of n: ");
int n=input.nextInt();
String primeNo = "";
int j=0;
System.out.println("Prime nums are:" );
for (int i = 1; i < n; i++){
for ( j = 2; j < i; j++){
if (i % j == 0) {
break;
}
}
if (i == j) {
primeNo += i + " ";
}
}
System.out.println(primeNo);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.