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)
Similar Reads
Program for centered nonagonal number Centered nonagonal number is a centered figurate number that represents a nonagon with a dot in the center and all other dots surrounding to the center dot in successive nonagonal layers. The centered nonagonal number for n is given by Centered nonagonal number = (3 * n - 2) * (3 * n - 1) / 2; https
6 min read
Armstrong Numbers between two integers A positive integer with digits a, b, c, d... is called an Armstrong number of order n if following condition is satisfied. abcd... = an + bn + cn + dn +... 153 = 1*1*1 + 5*5*5 + 3*3*3 = 1 + 125 + 27 = 153 Therefore, 153 is an Armstrong number. Examples: Input : 100 400 Output :153 370 371 Explanatio
6 min read
Program to find Cullen Number A Cullen Number is a number of the form is 2n * n + 1 where n is an integer. The first few Cullen numbers are 1, 3, 9, 25, 65, 161, 385, 897, 2049, 4609 . . . . . . Examples: Input : n = 4 Output :65 Input : n = 0 Output : 1 Input : n = 6 Output : 161 Below is implementation of formula. We use bitwi
3 min read
Program to check for Peterson number A number is said to be a Peterson number if the sum of factorials of each digit of the number is equal to the number itself. Example: Input : n = 145 Output = Yes Explanation: 145 = 5! + 4! + 1! = 120 + 24 +1 = 145 Input : n = 55 Output : No Explanation: 5! + 5! = 120 + 120 = 240 Since 55 is not equ
5 min read
XOR and OR of all N-digit Armstrong numbers Given an integer N, the task is to find the XOR and OR values of all N-digit Armstrong numbers. Examples Input: N = 3 Output: XOR = 271, OR = 511 153, 370, 371, 407 are the three digit armstrong numbers Input: N = 4 Output: XOR = 880, OR = 10098 Approach: Find the starting and ending number of N-dig
7 min read