Prime not prime in java

How to check if a number is prime in Java

In this answer, we will discuss how to check whether a given number is prime or not in Java.

Before that, let’s go over what a prime number is.

All numbers that are greater than one and have only two divisors, i.e., one and the number itself, are identified as prime numbers. We can also understand a prime number as a number that has exactly two divisors.

For example, let’s take the numbers two through seven:

  • Divisors of 2 are 1 and 2
  • Divisors of 3 are 1 and 3
  • Divisors of 4 are 1, 2, and 4
  • Divisors of 5 are 1 and 5
  • Divisors of 6 are 1, 2, 3, and 6
  • Divisors of 7 are 1 and 7

Two, three, five, and seven are numbers that have exactly two divisors, i.e., one and the respective number itself, so they are prime numbers. Four and six are non-prime numbers/composite numbers.

The isPrime() function

We can use the isPrime() function, which takes the number as input and returns true if the number is prime and false if it is not.

How to find a prime number in Java

  • Take a number as input.
  • Check if there is a divisor of the number in the range of [two, number/2] because two is the smallest prime number.
  • There is no number that can be completely divided by more than half of the number itself. We need to loop through two to the number itself divided by two (number/2).
  • If any divisor is found, then the number is not prime; otherwise, the given number is prime.
Читайте также:  Версия сайта для КПК

Code

Let’s look at an example that takes a number as input and returns a string that states whether the number is a prime number or a non-prime number. Enter a number and then press the ‘RUN’ button.

Источник

Java Program to Check Whether a Number is Prime or Not

To understand this example, you should have the knowledge of the following Java programming topics:

A prime number is a number that is divisible by only two numbers: 1 and itself. So, if any number is divisible by any other number, it is not a prime number.

Example 1: Program to Check Prime Number using a for loop

public class Main < public static void main(String[] args) < int num = 29; boolean flag = false; for (int i = 2; i > if (!flag) System.out.println(num + " is a prime number."); else System.out.println(num + " is not a prime number."); > >

In the above program, for loop is used to determine if the given number num is prime or not.

Here, note that we are looping from 2 to num/2. It is because a number is not divisible by more than its half.

Inside the for loop, we check if the number is divisible by any number in the given range (2. num/2) .

  • If num is divisible, flag is set to true and we break out of the loop. This determines num is not a prime number.
  • If num isn’t divisible by any number, flag is false and num is a prime number.

Example 2: Program to Check Prime Number using a while loop

public class Main < public static void main(String[] args) < int num = 33, i = 2; boolean flag = false; while (i ++i; > if (!flag) System.out.println(num + " is a prime number."); else System.out.println(num + " is not a prime number."); > >

In the above program, while loop is used instead of a for loop. The loop runs until i

Читайте также:  Видеоуроки по html программированию

Источник

Check Prime Number in Java [3 Methods]

Summary: In this tutorial, we will learn three different methods to check whether the given number is prime or not using the Java language.

A number is said to be a prime number if it is only divisible by 1 and itself.

Examples: 5, 7, 11, 17 etc.

Prime numbers from 1 to 100

Method 1: Using For Loop

In this method, we use for loop to iterate through the numbers between 1 and n/2 and check if any of them is a factor of the n.

If so, then then n is not prime, else it is a prime number.

import java.util.Scanner; public class Prime < public static void main(String args[])< Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); int num = in.nextInt(); boolean flag = true; //Assuming that input is Prime //Loop through all numbers between 1 and num for(int i=2; i<(num/2); i++)< if(num%i == 0)< /* *If num is divisible by any number other than 1 and itself- *then, it is Not Prime */ flag = false; break; //Since the num is not prime, so no use of iterating further >> if(flag == true) < System.out.println("It's Prime"); >else < System.out.println("Not Prime"); >> >

Enter a number: 7
It’s Prime

Method 2: Using Function

In this method, we write the logic used in the above method inside a separate function.

The function checks the number whether it is prime or not. If the number is prime it will return true otherwise false .

import java.util.Scanner; public class Prime < public static void main(String args[])< Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); int num = in.nextInt(); //pass the number to the function if(isPrime(num))< System.out.println("It's Prime"); >else < System.out.println("Not Prime"); >> private static boolean isPrime(int num) < boolean flag = true; for(int i=2; i<(num/2); i++)< if(num%i == 0)< //If num is divisible by i, it is not Prime. flag = false; break; >> return flag; > >

Enter a number: 3
It’s Prime

Читайте также:  Java как проверить regex

Method 3: Using Recursion

We can also check whether a number is prime or not using a recursive function.

To the recursive function, we pass the next potential factor as i and check if it is actually the factor of the number (i.e. n%i == 0 ), if so then we return false otherwise, we increment i and recursively call the function until i=n/2 .

If the value of i in the recursive call successfully reaches beyond n/2 , then the input number is prime otherwise not.

import java.util.*; class Main < public static void main(String args[])< Scanner in=new Scanner(System.in); System.out.print("Enter a number: "); int num = in.nextInt(); //call the recursive function with i=2 if(isPrime(num, 2)) System.out.println("It's Prime"); else System.out.println("Not a prime"); >//recursive function: num=number_to_be_checked, i=potential_factor_of_num private static boolean isPrime(int num, int i) < if(i==1 || i==2 || i>num/2) return true; else if(num%i == 0) return false; //recursive call return isPrime(num, i+1); > >

Enter a number: 7
It’s Prime

These were the three different ways to check prime number using the Java programming language.

Источник

Оцените статью