Program for Armstrong Numbers
Last Updated :
20 Jun, 2025
Given a number x, determine whether the given number is Armstrong's number or not. A positive integer of n digits is called an Armstrong number of order n (order is the number of digits) if
abcd... = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + ....
Here a, b, c and d are digits of input number abcd.....
Examples
Input: n = 153
Output: true
Explanation: 153 is an Armstrong number, 1*1*1 + 5*5*5 + 3*3*3 = 153
Input: n = 9474
Output: true
Explanation: 94 + 44 + 74 + 44 = 6561 + 256 + 2401 + 256 = 9474
Input: n = 123
Output: false
Explanation: 1³ + 2³ + 3³ = 1 + 8 + 27 = 36
[Approach 1] Naive Approach
- The idea is to first count the number of digits (or find the order). Let the number of digits be n.
- For every digit r in input number x, compute rn.
- If the sum of all such values is equal to x, then return true, else false.
C++
#include <iostream>
using namespace std;
// Function to calculate x raised
// to the power y
int power(int x, int y){
if (y == 0)
return 1;
if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
return x * power(x, y / 2) * power(x, y / 2);
}
int order(int n){
int t = 0;
while (n) {
t++;
n = n / 10;
}
return t;
}
// Function to check whether the given
// number is Armstrong number or not
bool armstrong(int n){
// Calling order function
int x = order(n);
int temp = n, sum = 0;
while (temp) {
int r = temp % 10;
sum += power(r, x);
temp = temp / 10;
}
return (sum == n);
}
int main()
{
int n = 153;
if(armstrong(n)){
cout << "true";
}else{
cout << "false";
}
return 0;
}
Java
class GfG {
// Function to calculate x raised to the power y
public static int power(int x, int y) {
if (y == 0)
return 1;
if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
return x * power(x, y / 2) * power(x, y / 2);
}
// Function to count number of digits
public static int order(int n) {
int t = 0;
while (n != 0) {
t++;
n = n / 10;
}
return t;
}
// Function to check whether the given number is Armstrong or not
public static boolean armstrong(int n) {
int x = order(n);
int temp = n, sum = 0;
while (temp != 0) {
int r = temp % 10;
sum += power(r, x);
temp = temp / 10;
}
return sum == n;
}
public static void main(String[] args) {
int n = 153;
if (armstrong(n)) {
System.out.println("true");
} else {
System.out.println("false");
}
}
}
Python
# Function to calculate x raised
# to the power y
def power(x, y):
if y == 0:
return 1
if y % 2 == 0:
return power(x, y // 2) * power(x, y // 2)
return x * power(x, y // 2) * power(x, y // 2)
# Function to count number of digits in n
def order(n):
t = 0
while n:
t += 1
n //= 10
return t
# Function to check whether the given
# number is Armstrong number or not
def armstrong(n):
# Calling order function
x = order(n)
temp = n
sum_ = 0
while temp:
r = temp % 10
sum_ += power(r, x)
temp //= 10
# If satisfies Armstrong condition
return sum_ == n
if __name__ == "__main__":
n = 153
if armstrong(n):
print("true")
else:
print("false")
C#
using System;
class GfG {
// Function to calculate x raised
// to the power y
static int power(int x, int y) {
if (y == 0)
return 1;
if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
return x * power(x, y / 2) * power(x, y / 2);
}
// Function to count number of digits in n
static int order(int n){
int t = 0;
while (n != 0)
{
t++;
n /= 10;
}
return t;
}
// Function to check whether the given
// number is Armstrong number or not
static bool armstrong(int n)
{
// Calling order function
int x = order(n);
int temp = n, sum = 0;
while (temp != 0)
{
int r = temp % 10;
sum += power(r, x);
temp /= 10;
}
// If satisfies Armstrong condition
return sum == n;
}
static void Main(string[] args)
{
int n = 153;
if (armstrong(n))
{
Console.WriteLine("true");
}
else
{
Console.WriteLine("false");
}
}
}
JavaScript
// Function to calculate x raised
// to the power y
function power(x, y) {
if (y === 0)
return 1;
if (y % 2 === 0)
return power(x, Math.floor(y / 2)) * power(x, Math.floor(y / 2));
return x * power(x, Math.floor(y / 2)) * power(x, Math.floor(y / 2));
}
// Function to count number of digits in n
function order(n) {
let t = 0;
while (n !== 0) {
t++;
n = Math.floor(n / 10);
}
return t;
}
// Function to check whether the given
// number is Armstrong number or not
function armstrong(n) {
// Calling order function
let x = order(n);
let temp = n, sum = 0;
while (temp !== 0) {
let r = temp % 10;
sum += power(r, x);
temp = Math.floor(temp / 10);
}
// If satisfies Armstrong condition
return sum === n;
}
// Driver Code
let n = 153;
if (armstrong(n)) {
console.log("true");
} else {
console.log("false");
}
Time Complexity: O(d*log(d)), where d is the number of digits in n, since we compute the power for each digit.
Space Complexity: O(1)
[Approach 2] Using Numeric Strings
The idea is to determine if a number is an Armstrong number by first converting it to a string to easily access its digits and count them. Each digit is then raised to the power of the total number of digits, and the results are summed. If this sum is equal to the original number, it is classified as an Armstrong number. This approach leverages simple string manipulation and power calculation to perform the check efficiently.
C++
#include <iostream>
#include <cmath>
using namespace std;
bool armstrong(int n)
{
// converting to string
string number = to_string(n);
n = number.length();
int output = 0;
for (char i : number)
output = output + (int)pow((i - '0'), n);
// check if equal to number
if (output == stoi(number))
return true;
else
return false;
}
int main()
{
int n = 153;
if(armstrong(n)){
cout << "true";
}else{
cout << "false";
}
}
Java
class GfG {
public static boolean armstrong(int n) {
// converting to string
String number = Integer.toString(n);
int length = number.length();
int output = 0;
for (char c : number.toCharArray()) {
output += (int) Math.pow(c - '0', length);
}
// check if equal to number
if (output == Integer.parseInt(number))
return true;
else
return false;
}
public static void main(String[] args) {
int n = 153;
if (armstrong(n)) {
System.out.println("true");
} else {
System.out.println("false");
}
}
}
Python
def armstrong(n):
# converting to string
number = str(n)
# number of digits
digits = len(number)
output = 0
# sum of each digit raised to the power of number of digits
for i in number:
output += int(i) ** digits
# check if equal to original number
return output == n
if __name__ == "__main__":
n = 153
if armstrong(n):
print("true")
else:
print("false")
C#
using System;
class GfG
{
static bool armstrong(int n)
{
// converting to string
string number = n.ToString();
int length = number.Length;
int output = 0;
foreach (char c in number)
{
output += (int)Math.Pow(c - '0', length);
}
// check if equal to number
if (output == int.Parse(number))
return true;
else
return false;
}
static void Main()
{
int n = 153;
if (armstrong(n))
{
Console.WriteLine("true");
}
else
{
Console.WriteLine("false");
}
}
}
JavaScript
function armstrong(n) {
// converting to string
let number = n.toString();
let length = number.length;
let output = 0;
for (let i of number) {
output += Math.pow(parseInt(i), length);
}
// check if equal to number
if (output === parseInt(number))
return true;
else
return false;
}
// Driver Code
let n = 153;
if (armstrong(n)) {
console.log("true");
} else {
console.log("false");
}
Time Complexity: O(d*log(d)), where d is the number of digits in n, since we compute the power for each digit.
Space Complexity: O(1)
Armstrong Numbers | DSA Problem
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem