C Program
#include<stdio.h> #include<math.h> int main() { int n, t, r, s = 0, d = 0; scanf("%d", &n); for (t = n; t; t /= 10) d++; for (t = n; t; t /= 10) { r = t % 10; s += pow(r, d); } printf(n == s ? "Armstrong" : "Not Armstrong"); }
C Output
Input: 153 Output: Armstrong
C++ Program
#include<iostream> #include<cmath> using namespace std; int main() { int n, t, r, s = 0, d = 0; cin >> n; for (t = n; t; t /= 10) d++; for (t = n; t; t /= 10) { r = t % 10; s += pow(r, d); } cout << (n == s ? "Armstrong" : "Not Armstrong"); }
C++ Output
Input: 9474 Output: Armstrong
JAVA Program
import java.util.*; class A { public static void main(String[] a) { int n = new Scanner(System.in).nextInt(), t = n, s = 0, d = 0; while (t > 0) { d++; t /= 10; } t = n; while (t > 0) { int r = t % 10; s += Math.pow(r, d); t /= 10; } System.out.print(n == s ? "Armstrong" : "Not Armstrong"); } }
JAVA Output
Input: 370 Output: Armstrong
Python Program
n = int(input()) d = len(str(n)) s = sum(int(i)**d for i in str(n)) print("Armstrong" if s == n else "Not Armstrong")
Python Output
Input: 407 Output: Armstrong
In-Depth Learning – Entire Concept in Paragraphs
What is an Armstrong Number?
An Armstrong number (or narcissistic number) is a number which is the sum of its own digits each raised to the power of the number of digits. For instance, 153 is an Armstrong number because:
1³ + 5³ + 3³ = 1 + 125 + 27 = 153.
This is a concept that applies primarily to three-digit numbers, though it applies to any number of digits. Some other examples are 370, 371, 9474, etc. Armstrong numbers are excellent for practicing decomposition of numbers, powers, and logic-based looping.
How the Code Works
We first find out how many digits the number contains through a loop or by converting the number to string. Then we retrieve every digit (through % 10), raise it to the power of the digit number, and sum the result. Afterwards, we verify if this sum is equal to the original number. If that's the case, the number is an Armstrong number.
Example
Consider number 153:
It contains 3 digits.
Digits: 1, 5, 3
Calculate: 1³ + 5³ + 3³ = 1 + 125 + 27 = 153 → Armstrong ✔
Now calculate 123:
It contains 3 digits.
Digits: 1, 2, 3
Calculate: 1³ + 2³ + 3³ = 1 + 8 + 27 = 36 → Not Armstrong ✖
Real-Life Analogy
Think of assigning each digit a duty to power it up with how many digits the entire number has. If they collaborate and their total strength perfectly replicates the original number, they are successful. It is similar to a superhero squad with each member possessing an individual power level, and collectively constructing their identity. If their total effort replicates who they are, then they're "true" (Armstrong); otherwise, they're impostors.
Why Armstrong Number Is Significant to Learn
This exercise educates on digit extraction, power computation, looping statements, and conditional statements. It's also an ideal one to learn modular arithmetic and control flow, and is frequently posed in interviews, entrance tests, and simple programming problems. It also aids in comprehending data types and accuracy in utilizing functions such as pow() in C/C++/Java or ** in Python.
Python-Specific Benefit
In Python, the answer is tidy and neat owing to string manipulation and list comprehensions. By passing the number through a conversion to a string, you can loop over digits with ease, use int(i)**d on each of them, and add them up — all in one line using sum().
If you're looking for the simplest method to determine whether a number is an Armstrong number, this tutorial offers succinct and concise examples in C, C++, Java, and Python. Armstrong numbers are ideal for developing logic and understanding digit manipulation, powers, and conditions checks. With code that works, real-world analogies, and mathematical stepdowns, this page provides a great reference for students, interview candidates, or programming enthusiasts. Whether you're getting ready for an exam or building your number theory skills, studying Armstrong number logic provides you with the foundation for more complex algorithmic thinking.
Social Plugin