How to Use Lambda Function in Java

How to find Armstrong number using Java

 An Armstrong number (also known as a narcissistic number, pluperfect digital invariant, or pluperfect number) is a number that is the sum of its own digits each raised to the power of the number of digits. In other words, an n-digit number is an Armstrong number if the sum of its digits, each raised to the power of n, is equal to the number itself.



For example, let's consider a 3-digit number like 153:


1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153


Since the sum is equal to the original number, 153 is an Armstrong number.


Here are a few more examples:


1634 is an Armstrong number because 1^4 + 6^4 + 3^4 + 4^4 = 1634.

9474 is an Armstrong number because 9^4 + 4^4 + 7^4 + 4^4 = 9474.

Armstrong numbers are named after Michael F. Armstrong, who first introduced them in a 1938 publication. These numbers are interesting mathematical curiosities and are often used as programming exercises.

package logical;

import java.util.*;

public class Armstrong {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter number :");
        int n = sc.nextInt();
        int num1 = n;
        double armnum = 0;

        while (n != 0) {
            armnum = armnum + Math.pow(n % 10, 3);
            n = n / 10;
        }
        System.out.println(armnum);

        if (num1 == armnum) {
            System.out.println("Armstrong Number");
        } else {
            System.out.println("Not Armstrong number");
        }

        sc.close();
    }

}



Comments