Write a program to check entered number is prime or not?
A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 that is not a prime number is called a composite number.
package com.tech.mindclues; import java.util.Scanner; public class PrimeNumber { public static void main(String arga[]) { System.out.println("Enter the number : "); Scanner sc = new Scanner(System.in); int n; Boolean flag = false; n = sc.nextInt(); for (int i = 2; i < n/2; i++) { if (n % i == 0) { flag = true; } } if (flag == true) { System.out.println("Entered number not a prime number : "); } else { System.out.println("Entered number is prime number :"); } } }
Enter the number : 7 Entered number is prime number :
post a comment