Armstrong Number in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

/* C - Armstrong Number check */
#include <stdio.h>
#include <math.h>

int main() {
    int n, temp, sum = 0, digits = 0;
    if (scanf("%d", &n) != 1) return 0;
    temp = n;
    while (temp) { digits++; temp /= 10; }
    temp = n;
    while (temp) {
        int d = temp % 10;
        sum += pow(d, digits);
        temp /= 10;
    }
    printf(sum == n ? "Armstrong\n" : "Not Armstrong\n");
    return 0;
}

C Output

Input:
153

Output:
Armstrong



C++ Program

// C++ - Armstrong Number check
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    int n, temp, sum = 0, digits = 0;
    if (!(cin >> n)) return 0;
    temp = n;
    while (temp) { digits++; temp /= 10; }
    temp = n;
    while (temp) {
        int d = temp % 10;
        sum += pow(d, digits);
        temp /= 10;
    }
    cout << (sum == n ? "Armstrong" : "Not Armstrong") << "\n";
    return 0;
}

C++ Output

Input:
9474

Output:
Armstrong



JAVA Program

// Java - Armstrong Number check
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        if (!sc.hasNextInt()) return;
        int n = sc.nextInt(), temp = n, digits = 0, sum = 0;
        while (temp > 0) { digits++; temp /= 10; }
        temp = n;
        while (temp > 0) {
            int d = temp % 10;
            sum += Math.pow(d, digits);
            temp /= 10;
        }
        System.out.println(sum == n ? "Armstrong" : "Not Armstrong");
    }
}

JAVA Output

Input:
9475

Output:
Not Armstrong



Python Program

# Python - Armstrong Number check
n = int(input().strip())
digits = len(str(n))
sum_val = sum(int(d)**digits for d in str(n))
print("Armstrong" if sum_val == n else "Not Armstrong")

Python Output

Input:
370

Output:
Armstrong



In-Depth Learning – Entire Concept in Paragraphs
Example:
An Armstrong number or a narcissistic number is a number in which the sum of its digits, each raised to the power equal to the number of digits, is equal to the number. For example, 153 is a three-digit number. If we compute
13+53+33=1+125+27=153 (These are power 3)
=1+125+27=153, so it equals the original number, making 153 Armstrong. The above programs do this by first determining how many digits the number contains, then looping through each digit, raising it to the power of the number of digits, and adding the outcomes. If the result is the same as the original number, we call it Armstrong; if not, it's not.

Real-Life Analogy:
 Conceptualize a security code wherein every digit has a "weight" corresponding to the number of digits in the code, and if you sum up all the weighted digits, you are back to your original code. Special numbers only meet this stringent property test — similar to Armstrong numbers being special in math.

Why It Matters:
Verifying Armstrong numbers is an excellent practice to learn about digit-by-digit number manipulation, loops, and mathematical functions such as exponentiation. It helps you learn to get digits with modulo and division, count digits in a number, and compare results to the original — skills that come up repeatedly in interview and competitive programming questions.

Insights to learn:
This exercise enforces integer manipulation without strings (except for the Python version for brevity). It also covers the use of powers in programming and demonstrates how to utilize functions such as pow() in C/C++/Java or the ** operator in Python. It's also a natural lead-in to similar topics such as perfect numbers, palindromic integers, and sum-of-factorials problems.

Interview and Project Relevance:
Armstrong number problems in interviews probe your knowledge of loops, conditionals, and number theory fundamentals. Employers can modify the problem — say, generating all Armstrong numbers in a range or for extremely large inputs — to challenge your optimization abilities. In projects, identical digit-by-digit verifications can be applied to validation algorithms, cryptographic checksum mechanisms, or number property-based puzzles.

SEO-friendly conclusion:
This complete Armstrong number guide shows working C, C++, Java, and Python programs to check if a number is Armstrong, explained with step-by-step logic, examples, and common pitfalls. You’ll learn how to extract digits, use powers, and compare computed sums efficiently. Whether you’re preparing for coding interviews, learning number theory concepts, or building algorithms that work on digit manipulation, mastering Armstrong number logic will boost your problem-solving skills and strengthen your programming fundamentals.