|
| 1 | +--- |
| 2 | +title: Java Program to Check Armstrong Number |
| 3 | +shortTitle: Check Armstrong Number |
| 4 | +description: Learn how to check if a number is an Armstrong number in Java using digit extraction and power calculation with multiple approaches and examples. |
| 5 | +--- |
| 6 | + |
| 7 | +An Armstrong number (also called a narcissistic number) is a number that equals the sum of its digits each raised to the power of the number of digits. For example: |
| 8 | + |
| 9 | +- 153 = 1³ + 5³ + 3³ = 1 + 125 + 27 = 153 (3 digits) |
| 10 | +- 9474 = 9⁴ + 4⁴ + 7⁴ + 4⁴ = 6561 + 256 + 2401 + 256 = 9474 (4 digits) |
| 11 | + |
| 12 | +Before you start, you may want to review: |
| 13 | + |
| 14 | +- [Java Variables and Literals](/docs/variables-and-literals) |
| 15 | +- [Java Operators](/docs/operators) |
| 16 | +- [Java Basic Input and Output](/docs/basic-input-output) |
| 17 | + |
| 18 | +## 1) Basic Armstrong Check |
| 19 | + |
| 20 | +This method extracts digits and calculates the sum of powers. |
| 21 | + |
| 22 | +### Example |
| 23 | + |
| 24 | +```java |
| 25 | +class Main { |
| 26 | + public static void main(String[] args) { |
| 27 | + int num = 153; // change value to test |
| 28 | + |
| 29 | + if (isArmstrong(num)) { |
| 30 | + System.out.println(num + " is an Armstrong number."); |
| 31 | + } else { |
| 32 | + System.out.println(num + " is not an Armstrong number."); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + static boolean isArmstrong(int num) { |
| 37 | + int original = num; |
| 38 | + int digits = countDigits(num); |
| 39 | + int sum = 0; |
| 40 | + |
| 41 | + while (num > 0) { |
| 42 | + int digit = num % 10; |
| 43 | + sum += Math.pow(digit, digits); |
| 44 | + num /= 10; |
| 45 | + } |
| 46 | + |
| 47 | + return original == sum; |
| 48 | + } |
| 49 | + |
| 50 | + static int countDigits(int num) { |
| 51 | + if (num == 0) return 1; |
| 52 | + int count = 0; |
| 53 | + while (num > 0) { |
| 54 | + count++; |
| 55 | + num /= 10; |
| 56 | + } |
| 57 | + return count; |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +#### Output (for num = 153) |
| 63 | + |
| 64 | +```plaintext |
| 65 | +153 is an Armstrong number. |
| 66 | +``` |
| 67 | + |
| 68 | +## 2) Optimized Version (Without Math.pow) |
| 69 | + |
| 70 | +Avoid floating-point operations by using integer multiplication. |
| 71 | + |
| 72 | +### Example |
| 73 | + |
| 74 | +```java |
| 75 | +class Main { |
| 76 | + public static void main(String[] args) { |
| 77 | + int num = 9474; // change value to test |
| 78 | + |
| 79 | + if (isArmstrongOptimized(num)) { |
| 80 | + System.out.println(num + " is an Armstrong number."); |
| 81 | + } else { |
| 82 | + System.out.println(num + " is not an Armstrong number."); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + static boolean isArmstrongOptimized(int num) { |
| 87 | + int original = num; |
| 88 | + int digits = countDigits(num); |
| 89 | + int sum = 0; |
| 90 | + |
| 91 | + while (num > 0) { |
| 92 | + int digit = num % 10; |
| 93 | + sum += power(digit, digits); |
| 94 | + num /= 10; |
| 95 | + } |
| 96 | + |
| 97 | + return original == sum; |
| 98 | + } |
| 99 | + |
| 100 | + static int power(int base, int exp) { |
| 101 | + int result = 1; |
| 102 | + for (int i = 0; i < exp; i++) { |
| 103 | + result *= base; |
| 104 | + } |
| 105 | + return result; |
| 106 | + } |
| 107 | + |
| 108 | + static int countDigits(int num) { |
| 109 | + if (num == 0) return 1; |
| 110 | + int count = 0; |
| 111 | + while (num > 0) { |
| 112 | + count++; |
| 113 | + num /= 10; |
| 114 | + } |
| 115 | + return count; |
| 116 | + } |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +#### Output (for num = 9474) |
| 121 | + |
| 122 | +```plaintext |
| 123 | +9474 is an Armstrong number. |
| 124 | +``` |
| 125 | + |
| 126 | +## 3) Find Armstrong Numbers in Range |
| 127 | + |
| 128 | +Program to find all Armstrong numbers within a given range. |
| 129 | + |
| 130 | +### Example |
| 131 | + |
| 132 | +```java |
| 133 | +import java.util.ArrayList; |
| 134 | +import java.util.List; |
| 135 | + |
| 136 | +class Main { |
| 137 | + public static void main(String[] args) { |
| 138 | + int start = 1; |
| 139 | + int end = 10000; |
| 140 | + |
| 141 | + System.out.println("Armstrong numbers between " + start + " and " + end + ":"); |
| 142 | + List<Integer> armstrongNumbers = findArmstrongInRange(start, end); |
| 143 | + |
| 144 | + for (int num : armstrongNumbers) { |
| 145 | + System.out.println(num + " (digits: " + countDigits(num) + ")"); |
| 146 | + } |
| 147 | + |
| 148 | + System.out.println("\nTotal Armstrong numbers found: " + armstrongNumbers.size()); |
| 149 | + } |
| 150 | + |
| 151 | + static List<Integer> findArmstrongInRange(int start, int end) { |
| 152 | + List<Integer> result = new ArrayList<>(); |
| 153 | + |
| 154 | + for (int i = start; i <= end; i++) { |
| 155 | + if (isArmstrong(i)) { |
| 156 | + result.add(i); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + return result; |
| 161 | + } |
| 162 | + |
| 163 | + static boolean isArmstrong(int num) { |
| 164 | + int original = num; |
| 165 | + int digits = countDigits(num); |
| 166 | + int sum = 0; |
| 167 | + |
| 168 | + while (num > 0) { |
| 169 | + int digit = num % 10; |
| 170 | + sum += power(digit, digits); |
| 171 | + num /= 10; |
| 172 | + } |
| 173 | + |
| 174 | + return original == sum; |
| 175 | + } |
| 176 | + |
| 177 | + static int power(int base, int exp) { |
| 178 | + int result = 1; |
| 179 | + for (int i = 0; i < exp; i++) { |
| 180 | + result *= base; |
| 181 | + } |
| 182 | + return result; |
| 183 | + } |
| 184 | + |
| 185 | + static int countDigits(int num) { |
| 186 | + if (num == 0) return 1; |
| 187 | + int count = 0; |
| 188 | + while (num > 0) { |
| 189 | + count++; |
| 190 | + num /= 10; |
| 191 | + } |
| 192 | + return count; |
| 193 | + } |
| 194 | +} |
| 195 | +``` |
| 196 | + |
| 197 | +#### Sample Output |
| 198 | + |
| 199 | +```plaintext |
| 200 | +Armstrong numbers between 1 and 10000: |
| 201 | +1 (digits: 1) |
| 202 | +2 (digits: 1) |
| 203 | +3 (digits: 1) |
| 204 | +4 (digits: 1) |
| 205 | +5 (digits: 1) |
| 206 | +6 (digits: 1) |
| 207 | +7 (digits: 1) |
| 208 | +8 (digits: 1) |
| 209 | +9 (digits: 1) |
| 210 | +153 (digits: 3) |
| 211 | +371 (digits: 3) |
| 212 | +407 (digits: 3) |
| 213 | +1634 (digits: 4) |
| 214 | +8208 (digits: 4) |
| 215 | +9474 (digits: 4) |
| 216 | +
|
| 217 | +Total Armstrong numbers found: 15 |
| 218 | +``` |
| 219 | + |
| 220 | +--- |
| 221 | + |
| 222 | +### Notes & Tips |
| 223 | + |
| 224 | +- **Common Armstrong numbers**: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 371, 407, 1634, 8208, 9474 |
| 225 | +- **Performance**: The optimized version using integer multiplication is faster than Math.pow() |
| 226 | +- **Large numbers**: For numbers with many digits, consider using `long` or `BigInteger` to avoid overflow |
| 227 | +- **Time complexity**: O(d) where d is the number of digits |
| 228 | +- **Space complexity**: O(1) for the basic check, O(n) for range finding |
0 commit comments