Next-Gen App & Browser
Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

Write a Java program to check whether a given number is Armstrong?

The Armstrong number is defined as the number whose sum of its digits raised to the power of the number of digits gives the number itself. A three-digit Armstrong number is 0, 1, 153, 370, 371, 407, etc., whereas a four-digit Armstrong number is 1634, 8208, 9474, etc.

  • 11= 1
  • 153: 13+ 53+ 3 3 = 1 + 125+ 27 = 153

Here is the Java program to check whether a given number is Armstrong.


import java.util.Scanner;
class CheckArmStrongNum {
    public static void main(String[] args) {
        int num1, k, sum = 0, num;   
        System.out.println("Enter the number:");
        Scanner sc = new Scanner(System. in );
        num = sc.nextInt();
        num1 = num;


        while (num!= 0)
        {
            k = num % 10;
            sum += Math.pow(k, 3);
            num /= 10;
        }
        if(sum == num1)
            System.out.println(num1+ " is an Armstrong number");
        else
            System.out.println(num1+ " is not an Armstrong number");
    }
}

Test your website on 3000+ browsers

Get 100 minutes of automation test minutes FREE!!

Test Now...

KaneAI - Testing Assistant

World’s first AI-Native E2E testing agent.

...
ShadowLT Logo

Start your journey with LambdaTest

Get 100 minutes of automation test minutes FREE!!

Signup for free