Prime number in Java - Java program to check prime number

Prime number in Java - Java program to check prime number

Java program to check prime number

A number which has only two factors 1 and by itself or we can say a number which is divisible by 1 and by itself. Say 5, it is divisible by 1, and by itself that is 5 is called Prime number.

 

Prime number in Java

In the following java code we have to check a number prime or not by dividing the number by 1 to the number itself. If the counter is equal to 2 then the entered number is prime otherwise not prime.

import java.util.*;

public class PrimeNumberTwofactor

{

    public static void main(String args[])

    {

        Scanner sc=new Scanner(System.in);

        int number, i, counter=0;

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

        number=sc.nextInt();

        for(i=1; i<=number; i++)

        {

            if(number % i == 0)

            {

                counter++;

            }

        }

        if(counter == 2)

        {

            System.out.println("Prime Number");

        }

        else

        {

            System.out.println("Not a Prime Number");

        }

    }

}

 

Prime number in java with the method

In the following code prime will be checked with a prime() method. One number is passed through prime(number) in the main() method by the user and after checking the number is divisible by other than 1 and the number itself. If divisible it will return 0 other 1.

import java.util.*;

public class PrimeNumber

{

    int i, num, flag;

    int prime(int n)

    {

        flag = 1;

        num=n;

        for(i=2; i<num; i++)

        {

            if(num % i == 0)

            {

                flag = 0;

                break;

            }

        }

        return flag; // flag variable return 1 or 0

    }

    public static void main(String args[])

    {

        Scanner sc=new Scanner(System.in);

        int number;

        int f;

        PrimeNumber ob = new PrimeNumber(); // object of the class

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

        number=sc.nextInt();

        f=ob.prime(number);

        if(f == 1) // check the flag variable 1 / 0

            System.out.println("Prime number");

        else

            System.out.println("Not a Prime number");

    }

}

Prime number in java with a method with boolean return

In the following piece of code prime will be checked with a prime () method and return value boolean. One number is passed through prime (number) in the main() method by the user and after checking the number is divisible by other than 1 and the number itself. If divisible it will return false other true.

 

import java.util.*;

public class PrimeNumberCheck

{

    int i, num;     // instance varaible

    boolean flag;   // flag variable (instance variable)

    boolean prime(int n)

    {

        flag = true;

        num = n;

        for(i=2; i<num; i++)

        {

            if(num % i == 0)

            {

                flag = false;

                break;

            }

        }

        return flag;

    }

 

    public static void main(String args[])

    {

        Scanner sc=new Scanner(System.in);

        int number;

        boolean f1; // boolean variable

        // object of class PrimeNumberCheck

        PrimeNumberCheck ob=new PrimeNumberCheck();

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

        number = sc.nextInt();

        f1 = ob.prime(number);      

        if(f1 == true ) // if 1 then prime or not prime

            System.out.println("Prime number");

        else

            System.out.println("Not a Prime number");

    }

}

 

More Java program

 

Armstrong Number in Java - What is an Armstrong number

Armstrong Number in Java - What is an Armstrong number

 

Armstrong Number in Java

Armstrong Number in Java

A number is said to be an Armstrong number when the summation of each digit cube of a number is equal to the original number.

Example 0, 1, 153, 370, 371, 407 etc.

Let’s check 153 is an Armstrong number or not.

153 = (1 * 1 * 1) + (5 * 5 * 5) + (3 * 3 * 3) 

153 = 1 + 125 + 27 is equal to 153.

Check another number 407 is an Armstrong number or not.

407 = (4 * 4 * 4) + (0 * 0 * 0) + (7 * 7 * 7)

407 =  64 + 0 + 343 is equal to 407 so it is an Armstrong

 

Java program to check Armstrong's number.

public class ArmstronNumber

{

    public static void main(String args[])

    {

        int num;       // Number to Check Armstrong no.

        int temp;      // temp variable to store the number

        int digit;  // last digit taker

        int arm=0;     // variable to store the cube of a digit

        num = 153;     // number to be checked

        temp = num;    // store the number

        while(num != 0)

        {

            digit = num % 10;

            arm  = arm + digit * digit * digit;

            num = num / 10;

        }

        if(temp == arm)

            System.out.println("Armstrong Number");

        else

            System.out.println("Not Armstrong number");

    }

}

In the above program, we have taken 153 as an example for checking the Armstrong number. We have taken each digit cube and summed it up to variable arm then check it, if it is equal to 153 then it is an Armstrong number.

 Java Program to Check Armstrong Number with method

import java.util.*;

public class ArmstronNumWithMethod

{

    int num;       // Number to Check Armstrong no.

    int digit;       // last digit taker

    int arm=0;   // variable to store the cube of a digit   

    // method

    int armstronNumber(int n)

    {

        num = n;

        while(num != 0)

        {

            digit = num % 10;

            arm  = arm + digit * digit * digit;

            num = num / 10;

        }

        return arm;

    }

    // main method started

    public static void main(String args[])

    {

        int number, armNum;

        Scanner sc = new Scanner(System.in);

        // object of the class

        ArmstronNumWithMethod ob = new ArmstronNumWithMethod();

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

        number = sc.nextInt();

        // call the method with number as parameter       

        armNum = ob.armstronNumber(number);

        if(number == armNum)

            System.out.println(number + " is Armstrong Number");

        else

            System.out.println(number + " is not Armstrong number");

    } // main method end

} // end of class

More Java program


How to Remove an Element from Array in Java with Example

How to Remove an Element from Array in Java with Example


Delete an element from an array

The following java program will delete the last element from the array. In the given program, the array contains these elements n[9, 8, 5, 4, 3, 7]. The element (4) having index 3 will be deleted after the execution of the program.
Output
Before Deletion                 
9 8 5 4 3 7                     
After Deletion                  
9 8 5 3 7      
So from the above output, we come to know that element 4 has been deleted.
The code is here:-

public class ElementDelArray
{
   public static void main(String args[])
   {
       int n[]={9, 8, 5, 4, 3, 7};
       int i, pos, l, s,k;
       l=n.length;
       s=4;
       System.out.println("Before Deletion");
       for(i=0; i<l; i++)
       {
           System.out.print(n[i] + " ");
        }
        pos = 0;
       for(i=0; i<l; i++)
       {
           if(n[i] == s)
           {
               pos = i;
               break;
            }
        }
        for(i=pos; i<l-1; i++)
        {
            n[i] = n[i+1];
           
        }
        System.out.println("\nAfter Deletion");
       for(i=0; i<l-1; i++)
       {
           System.out.print(n[i] + " ");
        }
    }
}
In the above program, we first find the position of the element (4), and then from that position, all the elements shifted one by one.