Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
228 changes: 228 additions & 0 deletions content/programs/java-program-to-check-armstrong-number.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
---
title: Java Program to Check Armstrong Number
shortTitle: Check Armstrong Number
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.
---

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:

- 153 = 1³ + 5³ + 3³ = 1 + 125 + 27 = 153 (3 digits)
- 9474 = 9⁴ + 4⁴ + 7⁴ + 4⁴ = 6561 + 256 + 2401 + 256 = 9474 (4 digits)

Before you start, you may want to review:

- [Java Variables and Literals](/docs/variables-and-literals)
- [Java Operators](/docs/operators)
- [Java Basic Input and Output](/docs/basic-input-output)

## 1) Basic Armstrong Check

This method extracts digits and calculates the sum of powers.

### Example

```java
class Main {
public static void main(String[] args) {
int num = 153; // change value to test

if (isArmstrong(num)) {
System.out.println(num + " is an Armstrong number.");
} else {
System.out.println(num + " is not an Armstrong number.");
}
}

static boolean isArmstrong(int num) {
int original = num;
int digits = countDigits(num);
int sum = 0;

while (num > 0) {
int digit = num % 10;
sum += Math.pow(digit, digits);
num /= 10;
}

return original == sum;
}

static int countDigits(int num) {
if (num == 0) return 1;
int count = 0;
while (num > 0) {
count++;
num /= 10;
}
return count;
}
}
```

#### Output (for num = 153)

```plaintext
153 is an Armstrong number.
```

## 2) Optimized Version (Without Math.pow)

Avoid floating-point operations by using integer multiplication.

### Example

```java
class Main {
public static void main(String[] args) {
int num = 9474; // change value to test

if (isArmstrongOptimized(num)) {
System.out.println(num + " is an Armstrong number.");
} else {
System.out.println(num + " is not an Armstrong number.");
}
}

static boolean isArmstrongOptimized(int num) {
int original = num;
int digits = countDigits(num);
int sum = 0;

while (num > 0) {
int digit = num % 10;
sum += power(digit, digits);
num /= 10;
}

return original == sum;
}

static int power(int base, int exp) {
int result = 1;
for (int i = 0; i < exp; i++) {
result *= base;
}
return result;
}

static int countDigits(int num) {
if (num == 0) return 1;
int count = 0;
while (num > 0) {
count++;
num /= 10;
}
return count;
}
}
```

#### Output (for num = 9474)

```plaintext
9474 is an Armstrong number.
```

## 3) Find Armstrong Numbers in Range

Program to find all Armstrong numbers within a given range.

### Example

```java
import java.util.ArrayList;
import java.util.List;

class Main {
public static void main(String[] args) {
int start = 1;
int end = 10000;

System.out.println("Armstrong numbers between " + start + " and " + end + ":");
List<Integer> armstrongNumbers = findArmstrongInRange(start, end);

for (int num : armstrongNumbers) {
System.out.println(num + " (digits: " + countDigits(num) + ")");
}

System.out.println("\nTotal Armstrong numbers found: " + armstrongNumbers.size());
}

static List<Integer> findArmstrongInRange(int start, int end) {
List<Integer> result = new ArrayList<>();

for (int i = start; i <= end; i++) {
if (isArmstrong(i)) {
result.add(i);
}
}

return result;
}

static boolean isArmstrong(int num) {
int original = num;
int digits = countDigits(num);
int sum = 0;

while (num > 0) {
int digit = num % 10;
sum += power(digit, digits);
num /= 10;
}

return original == sum;
}

static int power(int base, int exp) {
int result = 1;
for (int i = 0; i < exp; i++) {
result *= base;
}
return result;
}

static int countDigits(int num) {
if (num == 0) return 1;
int count = 0;
while (num > 0) {
count++;
num /= 10;
}
return count;
}
}
```

#### Sample Output

```plaintext
Armstrong numbers between 1 and 10000:
1 (digits: 1)
2 (digits: 1)
3 (digits: 1)
4 (digits: 1)
5 (digits: 1)
6 (digits: 1)
7 (digits: 1)
8 (digits: 1)
9 (digits: 1)
153 (digits: 3)
371 (digits: 3)
407 (digits: 3)
1634 (digits: 4)
8208 (digits: 4)
9474 (digits: 4)

Total Armstrong numbers found: 15
```

---

### Notes & Tips

- **Common Armstrong numbers**: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 371, 407, 1634, 8208, 9474
- **Performance**: The optimized version using integer multiplication is faster than Math.pow()
- **Large numbers**: For numbers with many digits, consider using `long` or `BigInteger` to avoid overflow
- **Time complexity**: O(d) where d is the number of digits
- **Space complexity**: O(1) for the basic check, O(n) for range finding
4 changes: 2 additions & 2 deletions src/app/(site)/about/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ export default function AboutPage() {
>
Our platform bridges complex Java concepts with real-world
application through interactive tutorials, hands-on exercises, and
structured learning paths. Whether you&apos;re a complete beginner or
experienced developer, our community-driven approach ensures
structured learning paths. Whether you&apos;re a complete beginner
or experienced developer, our community-driven approach ensures
accessible, practical learning for everyone.
</motion.p>
</div>
Expand Down
19 changes: 10 additions & 9 deletions src/app/(site)/legal/privacy/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ export default function PrivacyPolicyPage(): React.ReactElement {
<div className="prose prose-neutral dark:prose-invert max-w-none">
<h2>1. Introduction</h2>
<p>
Welcome to Javaistic (&quot;we,&quot; &quot;our,&quot; or &quot;us&quot;). We are committed to
protecting your privacy and ensuring transparency. This Privacy Policy
explains our practices regarding information handling on our open
source educational platform for learning Java programming.
Welcome to Javaistic (&quot;we,&quot; &quot;our,&quot; or
&quot;us&quot;). We are committed to protecting your privacy and
ensuring transparency. This Privacy Policy explains our practices
regarding information handling on our open source educational platform
for learning Java programming.
</p>
<p>
<strong>Important:</strong> Javaistic does not require user accounts
Expand Down Expand Up @@ -69,8 +70,8 @@ export default function PrivacyPolicyPage(): React.ReactElement {
If you choose to contribute to the Javaistic project through our
public GitHub repository, any information you provide (such as GitHub
username, email for commits, or pull request content) will be handled
according to GitHub&apos;s privacy policies and our open source licensing
terms.
according to GitHub&apos;s privacy policies and our open source
licensing terms.
</p>

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

<h2>8. International Users</h2>
Expand Down
29 changes: 16 additions & 13 deletions src/app/(site)/legal/terms/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ export default function TermsPage(): React.ReactElement {
<div className="prose prose-neutral dark:prose-invert max-w-none">
<h2>1. Acceptance of Terms</h2>
<p>
By accessing and using Javaistic (&quot;the Platform,&quot; &quot;we,&quot; &quot;our,&quot; or
&quot;us&quot;), an open source educational platform for learning Java
programming, you accept and agree to be bound by the terms and
provision of this agreement (&quot;Terms of Service&quot; or &quot;Terms&quot;). If you do
not agree to abide by these terms, please do not use this platform.
By accessing and using Javaistic (&quot;the Platform,&quot;
&quot;we,&quot; &quot;our,&quot; or &quot;us&quot;), an open source
educational platform for learning Java programming, you accept and
agree to be bound by the terms and provision of this agreement
(&quot;Terms of Service&quot; or &quot;Terms&quot;). If you do not
agree to abide by these terms, please do not use this platform.
</p>

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

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

<h3>8.2 Educational Content</h3>
Expand Down
4 changes: 2 additions & 2 deletions src/app/(site)/sponsors/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ export default function SponsorsPage() {
<h3 className="text-lg font-semibold">Growing Platform</h3>
</div>
<p className="text-muted-foreground">
Support a platform that&apos;s helping developers learn and grow in the
Java ecosystem.
Support a platform that&apos;s helping developers learn and grow
in the Java ecosystem.
</p>
<div className="mt-3 text-xs font-medium text-purple-600 dark:text-purple-400">
✓ Growing platform reach
Expand Down
Loading
Loading