In this tutorial, we will see “How to check if a number is an Armstrong Number using Java?”
Check if number is Armstrong number in java
Java – How to check if number is an Armstrong Number? Click To Tweet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | /** * Check Armstrong Number in Java * @author Deepak Verma * */ public class Check_If_Number_is_Armstrong_Number_Java8_Example { public static void main(String[] args) { int number = 250; if (isArmstrongNumber(number)) System.out.println(number + " is an Armstrong number."); else System.out.println(number + " is not an Armstrong number."); } public static boolean isArmstrongNumber(int number) { int originalNumber = number; int result = 0; int n = String.valueOf(number).length(); while (number != 0) { int digit = number % 10; result += Math.pow(digit, n); number /= 10; } return originalNumber == result; } } |
Output:
1 | 250 is not an Armstrong number. |
check if text or string present in a file using java 8
skip method java 8 stream
Java – How to check if number is an Armstrong Number? Click To Tweet
Do you like this Post? – then check my other helpful posts:
- Convert a Stream to a List in Java 8
- Stream maptoint in Java 8 with examples
- Double the numbers of specified ArrayList using Streams
- Double the even / odd numbers of a specified ArrayList using Streams
- How to check if Number is Prime or not using Streams
- Retrieve Even/Odd Numbers within the Range using Java 8 Streams
- How to add/sum up the ArrayList integers using Java8 Streams
- Generate Prime Numbers in Java 8 using Streams
- Comparator example – Collections sort with/without Lambda in Java 8
- How to pass function as a parameter in a method in Java 8?
- Remove duplicates from ArrayList in Java 8
- ForEach examples for Map/List in Java 8
Other Useful References: