← All resources

Write a program to input a number and print if it is a Double Armstrong number or not

ICSE Program

A double armstrong  number is a number that is equal to the sum of its digits where each digit is raised to the power of the number of digits in the number.

Eg:

153
370

371
407
1634
8208

9474

Explanation:

371 has 3 digits

Therefore,

Double Armstrong Example

9474 has 4 digits

Therefore,

Write a program in Java to determine if a number is a double armstrong number or not.

import java.util.*;
public class DoubleArmstrong
{
    public static void main()
    {
        Scanner sc = new Scanner(System.in);
        int n, c=0, d, s=0, copy;   //Declaring Variables
        System.out.println("Enter a number: ");
        n = sc.nextInt();           //Input number
        copy = n;                   //Storing a copy value
        while(n>0)                  //Counting number of digits
        {
            c++;
            n = n/10;
        }

        n = copy;                   //Storing original value back in n

        while(n>0)                  //Extracting digits
        {
            d = n%10;
            n = n/10;
            s = s+(int)Math.pow(d, c);  /*Adding the digits raised to the
            power of the number of digits and performing Type casting*/
        }
        if(s==copy)     //Checking if the sum matches the original number
            System.out.println("The number is a double armstrong number.");
        else
            System.out.println("The number is not a double armstrong number.");
    }
}