Write a java program to find the given number is prime or not ?
This java program check number is prime or not. Remember that smallest prime number is 2.
import java.util.Scanner; /** * * @author Mindclues */ public class Primeno { public static void main(String[] args) { int n; String result = ""; System.out.println("Enter a number --"); Scanner sc = new Scanner(System.in); n = sc.nextInt(); for (int i = 2; i < n; i++) { if (0 == n % i) { result = "It's not a prime number."; break; } else { result = "It's a prime number."; } } System.out.println(+n + " " + result); } }
Enter a number -- 8 8 It's not a prime number. Enter a number -- 7 7 It's a prime number.
post a comment