Skip to content

Commit 14f39c1

Browse files
authored
Merge pull request #759 from piyushkumar0707/feat/programs-armstrong-number
feat: add Java program to check Armstrong number [GSSoC 2025]
2 parents a38a0f2 + 934fdb8 commit 14f39c1

File tree

9 files changed

+280
-47
lines changed

9 files changed

+280
-47
lines changed
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
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

src/app/(site)/about/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ export default function AboutPage() {
142142
>
143143
Our platform bridges complex Java concepts with real-world
144144
application through interactive tutorials, hands-on exercises, and
145-
structured learning paths. Whether you&apos;re a complete beginner or
146-
experienced developer, our community-driven approach ensures
145+
structured learning paths. Whether you&apos;re a complete beginner
146+
or experienced developer, our community-driven approach ensures
147147
accessible, practical learning for everyone.
148148
</motion.p>
149149
</div>

src/app/(site)/legal/privacy/page.tsx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ export default function PrivacyPolicyPage(): React.ReactElement {
2121
<div className="prose prose-neutral dark:prose-invert max-w-none">
2222
<h2>1. Introduction</h2>
2323
<p>
24-
Welcome to Javaistic (&quot;we,&quot; &quot;our,&quot; or &quot;us&quot;). We are committed to
25-
protecting your privacy and ensuring transparency. This Privacy Policy
26-
explains our practices regarding information handling on our open
27-
source educational platform for learning Java programming.
24+
Welcome to Javaistic (&quot;we,&quot; &quot;our,&quot; or
25+
&quot;us&quot;). We are committed to protecting your privacy and
26+
ensuring transparency. This Privacy Policy explains our practices
27+
regarding information handling on our open source educational platform
28+
for learning Java programming.
2829
</p>
2930
<p>
3031
<strong>Important:</strong> Javaistic does not require user accounts
@@ -69,8 +70,8 @@ export default function PrivacyPolicyPage(): React.ReactElement {
6970
If you choose to contribute to the Javaistic project through our
7071
public GitHub repository, any information you provide (such as GitHub
7172
username, email for commits, or pull request content) will be handled
72-
according to GitHub&apos;s privacy policies and our open source licensing
73-
terms.
73+
according to GitHub&apos;s privacy policies and our open source
74+
licensing terms.
7475
</p>
7576

7677
<h2>4. How We Use Information</h2>
@@ -129,9 +130,9 @@ export default function PrivacyPolicyPage(): React.ReactElement {
129130
<p>
130131
Javaistic is an educational platform intended for learners of all ages
131132
interested in Java programming. Since we do not collect personal
132-
information, there are no special considerations for children&apos;s data.
133-
However, we recommend that children under 13 have parental guidance
134-
when accessing technical content.
133+
information, there are no special considerations for children&apos;s
134+
data. However, we recommend that children under 13 have parental
135+
guidance when accessing technical content.
135136
</p>
136137

137138
<h2>8. International Users</h2>

src/app/(site)/legal/terms/page.tsx

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ export default function TermsPage(): React.ReactElement {
2121
<div className="prose prose-neutral dark:prose-invert max-w-none">
2222
<h2>1. Acceptance of Terms</h2>
2323
<p>
24-
By accessing and using Javaistic (&quot;the Platform,&quot; &quot;we,&quot; &quot;our,&quot; or
25-
&quot;us&quot;), an open source educational platform for learning Java
26-
programming, you accept and agree to be bound by the terms and
27-
provision of this agreement (&quot;Terms of Service&quot; or &quot;Terms&quot;). If you do
28-
not agree to abide by these terms, please do not use this platform.
24+
By accessing and using Javaistic (&quot;the Platform,&quot;
25+
&quot;we,&quot; &quot;our,&quot; or &quot;us&quot;), an open source
26+
educational platform for learning Java programming, you accept and
27+
agree to be bound by the terms and provision of this agreement
28+
(&quot;Terms of Service&quot; or &quot;Terms&quot;). If you do not
29+
agree to abide by these terms, please do not use this platform.
2930
</p>
3031

3132
<h2>2. Description of Service</h2>
@@ -86,8 +87,8 @@ export default function TermsPage(): React.ReactElement {
8687
<li>Upload viruses, malware, or other harmful code</li>
8788
<li>
8889
Post content that is unlawful, harmful, threatening, abusive,
89-
harassing, defamatory, vulgar, obscene, or invasive of another&apos;s
90-
privacy
90+
harassing, defamatory, vulgar, obscene, or invasive of
91+
another&apos;s privacy
9192
</li>
9293
<li>
9394
Impersonate any person or entity or misrepresent your affiliation
@@ -97,9 +98,10 @@ export default function TermsPage(): React.ReactElement {
9798
<h3>4.2 Educational Use</h3>
9899
<p>
99100
The Platform is intended for educational purposes. While we strive to
100-
provide accurate information, all content is provided &quot;as is&quot; for
101-
learning purposes. Users should verify information independently and
102-
use proper judgment when applying concepts learned here.
101+
provide accurate information, all content is provided &quot;as
102+
is&quot; for learning purposes. Users should verify information
103+
independently and use proper judgment when applying concepts learned
104+
here.
103105
</p>
104106

105107
<h2>5. Content and Contributions</h2>
@@ -197,9 +199,10 @@ export default function TermsPage(): React.ReactElement {
197199
<h2>8. Disclaimers</h2>
198200
<h3>8.1 Service Availability</h3>
199201
<p>
200-
The Platform is provided on an &quot;as is&quot; and &quot;as available&quot; basis. As an
201-
open source project maintained by volunteers, we do not guarantee that
202-
the Platform will be uninterrupted, timely, secure, or error-free.
202+
The Platform is provided on an &quot;as is&quot; and &quot;as
203+
available&quot; basis. As an open source project maintained by
204+
volunteers, we do not guarantee that the Platform will be
205+
uninterrupted, timely, secure, or error-free.
203206
</p>
204207

205208
<h3>8.2 Educational Content</h3>

src/app/(site)/sponsors/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ export default function SponsorsPage() {
261261
<h3 className="text-lg font-semibold">Growing Platform</h3>
262262
</div>
263263
<p className="text-muted-foreground">
264-
Support a platform that&apos;s helping developers learn and grow in the
265-
Java ecosystem.
264+
Support a platform that&apos;s helping developers learn and grow
265+
in the Java ecosystem.
266266
</p>
267267
<div className="mt-3 text-xs font-medium text-purple-600 dark:text-purple-400">
268268
✓ Growing platform reach

0 commit comments

Comments
 (0)