Java Tutorial
This java program is used to find the number is prime number or not.
A number greater than 1 is called a prime number, if it has exactly two factors, namely 1 and the number itself.
import java.util.Scanner; import java.lang.*; public class PrimeNumber { public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println("Checks Prime Number or not"); System.out.println("---------------------------"); System.out.print("Enter Number: "); int number = in.nextInt(); boolean isPrime = true; for (int i=2;i<number;i++){ if(number % i == 0) { isPrime = false; } } if (isPrime) { System.out.println("Number '"+number+"' is a Prime Number!"); } else { System.out.println("Number '"+number+"' is not a Prime Number!"); } } }Output:
D:\Java_Programs>javac PrimeNumber.java D:\Java_Programs>java PrimeNumber Checks Prime Number or not --------------------------- Enter Number: 4 Number '4' is not a Prime Number! D:\Java_Programs>javac PrimeNumber.java D:\Java_Programs>java PrimeNumber Checks Prime Number or not --------------------------- Enter Number: 13 Number '13' is a Prime Number!
import java.util.Scanner; import java.lang.*; public class PrimeNumber { public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println("Checks Prime Number or not"); System.out.println("---------------------------"); System.out.print("Enter Number: "); int number = in.nextInt(); boolean isPrime = true; int i=2; while (i<number){ if(number % i == 0) { isPrime = false; } i++; } if (isPrime) { System.out.println("Number '"+number+"' is a Prime Number!"); } else { System.out.println("Number '"+number+"' is not a Prime Number!"); } } }Output:
D:\Java_Programs>javac PrimeNumber.java D:\Java_Programs>java PrimeNumber Checks Prime Number or not --------------------------- Enter Number: 19 Number '19' is a Prime Number! D:\Java_Programs>javac PrimeNumber.java D:\Java_Programs>java PrimeNumber Checks Prime Number or not --------------------------- Enter Number: 21 Number '21' is not a Prime Number!
This java program is used to display the list of prime numbers till the limit specified as user input.
import java.util.Scanner; import java.lang.*; public class PrimeNumber { public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println("Prime Numbers List"); System.out.println("---------------------------"); System.out.print("Enter Limit: "); int limit = in.nextInt(); System.out.println("Prime Numbers:"); for(int i = 2;i <= limit;i++) { boolean isPrime = true; for(int j = i-1; j > 1 ; j--) { if(i%j == 0){ isPrime = false; } } if (isPrime) { System.out.print(i+"\t"); } } } }Output:
D:\Java_Programs>javac PrimeNumber.java D:\Java_Programs>java PrimeNumber Prime Number List -------------------- Enter Limit: 20 Prime Numbers: 2 3 5 7 11 13 17 19
import java.util.Scanner; import java.lang.*; public class PrimeNumber { public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println("Prime Numbers List"); System.out.println("---------------------------"); System.out.print("Enter Limit: "); int limit = in.nextInt(); System.out.println("Prime Numbers:"); int i = 2; while (i <= limit) { int j = i-1; boolean isPrime = true; while ( j > 1) { if(i%j == 0){ isPrime = false; } j--; } if (isPrime) { System.out.print(i+"\t"); } i++; } } }Output:
D:\Java_Programs>javac PrimeNumber.java D:\Java_Programs>java PrimeNumber Prime Number List -------------------- Enter Limit: 25 Prime Numbers: 2 3 5 7 11 13 17 19 23
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page