armstrong no logic in java code example
Example: armstrong numbers problem java
import java.util.Scanner;
public class ArmstrongNumberExample {
public static void main(String[] args) {
int sum = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number: ");
int input = in.nextInt();
String val = String.valueOf(input);
char[] charArray = val.toCharArray();
int[] numArray = new int[charArray.length];
for(int i=0; i<charArray.length; i++) {
numArray[i] = getPowerOfNumber(Integer.parseInt(String.valueOf(charArray[i])), charArray.length);
sum = sum + numArray[i];
}
if(sum == input) {
System.out.println("Entered number is an Armstrong number.");
}else {
System.out.println("Entered number is NOT an Armstrong number.");
}
in.close();
}
public static int getPowerOfNumber(int num, int count) {
return (int) Math.pow(num, count);
}
}