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:
153Output:
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:
9474Output:
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:
9475Output:
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:
370Output:
Armstrong
In-Depth Learning – Entire Concept in Paragraphs
Social Plugin