3 approaches for prime number program in java | codeYourslf

In this section you can learn how to write a prime number program in java with 3 different approaches. Code yourself, java for Beginners, basic java

prime number program in java  codeYourslf

3 approaches for prime number program in java | codeYourslf

In this section you can learn how to write a prime number program in java with 3 different approaches.

Approach:-1 "java program to check prime number using for loop"

 public class UsingforLoop {

    public static void main(String[] args){

      int num = 13,count = 0;

      if(num == 0 || num == 1){

        System.out.println("It is not prime number");

      }

      else{

      for(int i = 2; i < num/2; i++){

       if(num % i == 0){

         count += 1;

         break;

       } 

      }

      if(count == 0){

        System.out.println("It is a prime number");

      }

      else{

        System.out.println("It is not prime number");

      }

      }

    }

} 

 
+
Explanation

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

Ex :- 7, if we tried to divide 7 with other numbers it must give some reminder it means 7 is not completely divisible by other numbers except 1 and itself i.e 7

In programming we can check prime of a number by using that modulus operator, which returns the reminder of any number. 

Here inside for loop we have started dividing the number from 2 upto the previous of that number. And in if else part we are checking for divisibility by these numbers using modulus operator.  

If number satisfied num % i == 0, then the number is not prime. Because it is divisible by more than two numbers.

Output

3 approaches for prime number program in java | codeYourslf

Approach:-2 "prime number program in java using method"

 public class usingMethod {

    public static void main(String[] args){

      isPrime(2);

      isPrime(15);

      isPrime(19);

    }

    

    public static void isPrime(int num){

      if(num == 0 || num == 1){

        System.out.println("This is not prime number");

      }

      else{

        int count = 0;

        for(int i = 2; i < num/2; i++){

          if(num % i == 0){

            System.out.println("It is not a prime number");

            count += 1;

            break;

          }

        }

        if(count == 0){

          System.out.println("The number is prime");

        }

      }

    } 

    

}

 
+
Explanation

In this program I have used method concept to check whether a number is primer or not

Here I have declared a static method and inside that I have used while loop to check the logic. Here logic remain same for this problem also.

Approach:-3 "check prime number in java with user input by scanner class"

import java.util.Scanner;

public class primeNumberScanner {

  public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter the number: ");

    int num = sc.nextInt();

    checkPrimeNumber cp = new checkPrimeNumber();

    boolean result = cp.isPrimeNum(num);

    if (result) {

      System.out.println("This is prime number");

    } else {

      System.out.println("This is not prime numer");

    }

  }

}

class checkPrimeNumber {

  public boolean isPrimeNum(int num) {

    if (num == 0 || num == 1) {

      return false;

    } else {

      for (int i = 2; i < num / 2; i++) {

        if (num % i == 0) {

          return false;

        }

      }

      return true;

    }

  }

}

  
+
Explanation

In this program I have used another concept of method and here I also use the user input concept for prime number program in java.

 Here I have used a separate class i.e checkPrime, inside that I have declared a Boolean method named isPrimeNumber()

If the condition satisfy I have returned true otherwise false. And in the main method I have received that return value in a result variable.

 And using ternary operator I have checked result is true or false. If result become true I have printed this is a prime number, in the other hand number is not prime.

Here ternary operator works like if else statement.


If you learn something new from this article please share with your friends and write a sweet comments below .

Hey if you have any questions or any doubt, feel free to write it on the comment below or contact me. I will definitely try my best to solve your query.

`

Post a Comment

Hey Please 👏, feel free to share your opinion 🌍
© codeYourslf. All rights reserved. Distributed by Pixabin