This is my code written to check whether a given number is armstrong or not . but the logic differs from the conventional logic of using integer and separating its digits and calculating , instead i optimize the logic of separating the digits using Sting and char array . Please review my code ?
import java.util.Scanner;
public class ArmstrongNumberChecker {
public static void main(String[] args) {
int number;
int sum = 0;
Scanner scanner = new Scanner(System.in);
try {
System.out.println("\nEnter the number to check if it is armstrong number\n");
number = scanner.nextInt();
String s = Integer.toString(number);
char[] c = s.toCharArray();
for (int i = 0; i < s.length(); i++) {
sum = ( int ) (sum + Math.pow((c[i] - 48), c.length));
}
if (sum == number) {
System.out.println("Number " + number + " is Armstrong");
} else {
System.out.println("Number " + number + " is not Armstrong");
}
} catch (Exception e) {
System.out.println("Invalid data");
}
}
}