Write a program in java to check whether the number is prime or not.
condition - using' for 'loop.
The best answer will be marked as brainliest.
Answers
/*
Project Type: Brainly Answer
Date Created: 11-02-2021
Date Edited Last Time: ---NIL---
Question Link: https://brainly.in/question/35016338
Question: Write a program in java to check whether the number is prime or not.
Condition - using 'for' loop.
Program Created By: atrs7391
Programming Language: Java
Language version (When program created or last edited): jdk-15.0.2
*/
package Brainly_Answers;
import java.util.Scanner;
public class Prime_Check_Using_For_Loop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
long n = sc.nextLong();
boolean sign = false;
for (int i = 2; i <= n/2; ++i) {
if (n % i == 0) {
sign = true;
break;
}
}
if (n == 0 || n == 1) {
System.out.println("Given number is not a prime number. ");
}
else if (!sign) {
System.out.println("Given number is a prime number. ");
}
else {
System.out.println("Given number is not a prime number. ");
}
}
}