Given a number x, check if 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 = 153Input: n = 9474
Output: true
Explanation: 94 + 44 + 74 + 44 = 6561 + 256 + 2401 + 256 = 9474Input: n = 123
Output: false
Explanation: 1Âģ + 2Âģ + 3Âģ = 1 + 8 + 27 = 36
Table of Content
Naive Approach
- 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.Â
#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;
}
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");
}
}
}
# 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")
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");
}
}
}
// 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");
}
Output
true
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)
Using Numeric Strings
- Convert 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.
#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";
}
}
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");
}
}
}
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")
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");
}
}
}
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");
}
Output
true
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)