Friday, 18 June 2021

How to check whether a given number is Armstrong Number or not?

What is an Armstrong Number?

An Armstrong Number is a number, where the sum of cubes of each digit of that 
number is equal to the given number.
Example: 371 = 33 + 73 + 13
----------------------------------------------------------------------------------------------------------------------
package com.algos;

import java.util.Scanner;

public class ArmstrongNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(checkArmstrongNumber(n));
}

private static boolean checkArmstrongNumber(int n) {
int movingCubeTot = 0;
int temp = n;
while(temp > 0) {
int currDig = temp % 10;
temp = temp/10;
movingCubeTot = movingCubeTot + (currDig * currDig * currDig);
}
return n == movingCubeTot;
}
}

No comments:

Post a Comment