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.

Nearest prime pair number in java - Program to print nearest prime for a given number

Nearest prime pair number in java - Program to print nearest prime for a given number

 

Nearest prime pair number in java

Nearest prime pair number in java - Program to print the nearest prime for a given number

In the following program, it generates a nearest prime number of a given number. This program takes a number in the main() method and then it passed to a for loop and from there it checks the prime number with its closest. Suppose when we enter 15 it will print the nearest prime pair number as follows:

11, 13, and 17, 19.

import java.util.*;

class NearestPrimeNumber

{

    public boolean isPrime(int number)

    {

        int i,flag = 1;

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

        {

            if(number % i==0)

            {

                flag = 0;

                break;

            }

        }

        if(flag == 1)

        {

            return(true);

        }

        else

            return(false);

    }

    public static void main(String args[])

    {

        Scanner in=new Scanner(System.in);

        NearestPrimeNumber ob=new NearestPrimeNumber();

        int i,m,num,temp = 0;

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

        num=in.nextInt();

        for(i=num-1;i>=1;i--)

        {

            if(ob.isPrime(i))

            {

                temp = i + 2;

                if(ob.isPrime(temp))

                {

                    System.out.println("nearest prime pair is");

                    System.out.println(i + " , "+ temp);

                    break;

                }

            }

        }

        for(i=num ; ; i++)

        {

            if(ob.isPrime(i))

            {

                m=i-2;

                if(ob.isPrime(m))

                {

                    if(temp != i)

                    {

                        System.out.println(m+" , "+i);

                        break;

                    }

                    else

                    {

                        break;

                    }

                }

            }

        }

    }    

}

More Java program


Perfect number in java - Write a program to find perfect number or not

Perfect number in java - Write a program to find perfect number or not

 

Perfect number in java

Perfect number in java

A perfect number is a positive number (integer number) is the sum total of its factor except for the number itself. For example 6, has factor 1, 2, 3 (except 6) and add it all 1 + 2 + 3 = 6. Here summation of its divisors (excluding the number) is equal to the original number.

In the following code a number is taken by the user in n = sc.nextInt() and then with With the help of for loop, we do a summation of its divisor, less them the number. After then with the help of if condition statement

        if(s == n)

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

        else

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

We check the number is equal to the calculated summation in s. if both are equal then the number is the perfect number.

 

import java.util.*;

public class PerfectNumber

{

    public static void main(String args[])

    {

        int i, s=0, n;

        Scanner sc=new Scanner(System.in);

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

        n=sc.nextInt();

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

        {

            if(n % i == 0)

                s = s + i;

        }

        if(s == n)

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

        else

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

    }

}

Perfect number in java with the method.

The below program is done with the help of a method

int perfectNumber(int num)

    {

        sum = 0;

        number = num;

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

        {

            if(number % i == 0)

                sum =sum + i;

        }

        return sum;

    }

In the above piece of code we do the same calculation as above but within a block called perfectNumber(int num) where the supplied number is sum up of its factor (except the number itself)  and return to the value. Then in the main() method, it checked the original number and if it is equal to the return value then that number is a perfect number.

import java.util.*;

public class PerfectNumberMethod

{

    private int i;      // for iteraion

    private int sum;    // for summation of factor

    private int number; // number to check perfect number

    int perfectNumber(int num)

    {

        sum = 0;

        number = num;

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

        {

            if(number % i == 0)

                sum =sum + i;

        }

        return sum;

    } // end of perfect method

    // main method started

    public static void main(String args[])

    {

        int i, s, n;

        PerfectNumberMethod ob = new PerfectNumberMethod();

        Scanner sc=new Scanner(System.in);

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

        N = sc.nextInt();

        s = ob.perfectNumber (n);

        if(s == n)

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

        else

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

    }

}

Perfect number in java using recursion

import java.util.*;

public class PerfectNumberWithRecursion

{

    int sum, number, i;   

    PerfectNumberWithRecursion()

    {

        sum = 0;

        i = 1;

    }

 

    int perfectNumber(int num)

    {

        number = num;

        if(i<=number/2)

        {

            if(number%i==0)

            {

                sum =sum + i;

            }

            i++;

            perfectNumber(number);

        }

        return sum;   

    }

    // main method started

    public static void main(String arg[])  

    {

        int n, s;

        Scanner sc=new Scanner(System.in);      

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

        n=sc.nextInt();

        PerfectNumberWithRecursion ob=new PerfectNumberWithRecursion( );

        s = ob.perfectNumber(n);

        if(s == n)

            System.out.println(n + " is a perfect number");

        else

            System.out.println(n + " is not a  perfect number");

    }

}

More Java program