Computer Science, asked by ankita711, 4 months ago

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

Answered by atrs7391
3

/*

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. ");

       }

   }

}

Answered by Oreki
6

\textsf{\large \textbf{Algorithm}}

   \text{\textemdash \:\: Accepting the number using the \textbf{Scanner} class.}\\\text{\textemdash \:\: Iterating from 2 to number and find any factors other than 1 and itself.}\\\text{\textemdash \:\: If the number has factors other than 1 and itself then, false is returned.}\\\text{\textemdash \:\: The appropriate message is displayed according to the value received.}

Attachments:
Similar questions