Java – ArmStrong Number

Check the given 3 digit number is Armstrong Number or not :

  • Sum of cubes of individual digits equals to the same number
  • Example:        153 -> 1^3 + 5^3 + 3^3 = 153
import java.util.Scanner;
class Code
{
            public static void main(String[] args) {
                        Scanner scan = new Scanner(System.in);
                        System.out.print(“Enter Num : “);
                        int n = scan.nextInt();
                        int temp, sum=0, r;
                        temp=n;
                        while(n>0){
                                    r = n%10;
                                    sum = sum + r*r*r;
                                    n = n/10;
                        }
                        if(temp==sum)
                                    System.out.println(“ArmStrong Number”);
                        else
                                    System.out.println(“Not an ArmStrong Number”);
            }
}

Program to check the given number is Armstrong number or not:

  • Armstrong Number: Sum of its own digits raised to power number of digits
  • Examples :     
    • 153 = 1^3 + 5^3 + 3^3 = 153
    • 1634 = 1^4 + 6^4 + 3^4 + 4^4 = 1634
import java.util.Scanner;
class Code
{
            public static void main(String[] args) {
                        int n, r, sum=0, temp, c=0, s, i;
                        Scanner scan = new Scanner(System.in);
                        System.out.print(“Enter Num : “);
                        n = scan.nextInt();
                        temp=n;
                        while(n>0){
                                    n=n/10;          
                                    c++;
                        }
                        n=temp;
                        while(n>0){
                                    r = n%10;
                                    s=1;
                                    for(i=1 ; i<=c ; i++){
                                                s = s*r;
                                    }
                                    sum = sum + s;
                                    n = n/10;
                        }
                        if(temp==sum)
                                    System.out.println(“ArmStrong Number”);
                        else
                                    System.out.println(“Not an ArmStrong Number”);
            }
}
Scroll to Top