Java Program to Display Prime Numbers
Today i will write two applications.One application will display prime numbers from 1 to 200 and the other application i will not specify the end number i.e from 1 to n.
2.
1.Displaying Prime numbers from 1 to 200 in java
[
public class DisplayPrimeNumbers { public static void main(String args[]) { int maximum=200; System.out.println("This application prints numbers between 1 and"+maximum); for(int j=1;j<=200;j++) { boolean ifPrime=true; for(int i=2;i<j;i++) { if(j%i==0) { ifPrime=false; break; } } if(ifPrime) { System.out.println(j +" is a prime number"); } } } }
]
2.
1.Displaying Prime numbers from 1 to n in Java
[
import java.util.Scanner; public class DisplayPrimeNumbers { public static void main(String args[]) { Scanner input = new Scanner(System.in); System.out.println("Enter N value"); int maximum = input.nextInt(); System.out.println("This application prints numbers between 1 and" + maximum); for (int j = 1; j <= maximum; j++) { boolean ifPrime = true; for (int i = 2; i < j; i++) { if (j % i == 0) { ifPrime = false; break; } } if (ifPrime) { System.out.println(j + " is a prime number"); } } } }
]