Given a number **N**, the task is to check whether the given number is Armstrong number or not. If the given number is Armstrong Number then print **“Yes”** else print **“No”**.

A positive integer of

Ddigits is called an armstrong-numbers of orderD(order is the number of digits) if

WhereDis number of digit in numberN

and N(1), N(2), N(3)… are digit of numberN.

**Examples:**

Input:N = 153

Output:Yes

Explanation:

153 is an Armstrong number.

1*1*1 + 5*5*5 + 3*3*3 = 153

Input:120

Output:No

Explanation:

120 is not an Armstrong number.

1*1*1 + 2*2*2 + 0*0*0 = 9

**Approach:** The idea is to count the number of digits(say **d**) in the given number **N**. For every digit(say **r**) in the given number **N** find the value of **r ^{d}** and if the summation of all the values is

**N**then print

**“Yes”**else print

**“No”**.

Below is the implementation of the above approach:

## C

`// C program to find Armstrong number ` `#include <stdio.h> ` ` ` `// Function to calculate N raised ` `// to the power D ` `int` `power(` `int` `N, unsigned ` `int` `D) ` `{ ` ` ` `if` `(D == 0) ` ` ` `return` `1; ` ` ` ` ` `if` `(D % 2 == 0) ` ` ` `return` `power(N, D / 2) ` ` ` `* power(N, D / 2); ` ` ` ` ` `return` `N * power(N, D / 2) ` ` ` `* power(N, D / 2); ` `} ` ` ` `// Function to calculate the order of ` `// the number ` `int` `order(` `int` `N) ` `{ ` ` ` `int` `r = 0; ` ` ` ` ` `// For each digit ` ` ` `while` `(N) { ` ` ` `r++; ` ` ` `N = N / 10; ` ` ` `} ` ` ` `return` `r; ` `} ` ` ` `// Function to check whether the given ` `// number is Armstrong number or not ` `int` `isArmstrong(` `int` `N) ` `{ ` ` ` `// Calling order function ` ` ` `int` `D = order(N); ` ` ` ` ` `int` `temp = N, sum = 0; ` ` ` ` ` `// For each digit ` ` ` `while` `(temp) { ` ` ` `int` `Ni = temp % 10; ` ` ` `sum += power(Ni, D); ` ` ` `temp = temp / 10; ` ` ` `} ` ` ` ` ` `// If satisfies Armstrong condition ` ` ` `if` `(sum == N) ` ` ` `return` `1; ` ` ` `else` ` ` `return` `0; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given Number N ` ` ` `int` `N = 153; ` ` ` ` ` `// Function Call ` ` ` `if` `(isArmstrong(N) == 1) ` ` ` `printf` `(` `"True\n"` `); ` ` ` `else` ` ` `printf` `(` `"False\n"` `); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## C++

`// C++ program to find Armstrong number ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate N raised ` `// to the power D ` `int` `power(` `int` `N, unsigned ` `int` `D) ` `{ ` ` ` `if` `(D == 0) ` ` ` `return` `1; ` ` ` ` ` `if` `(D % 2 == 0) ` ` ` `return` `power(N, D / 2) ` ` ` `* power(N, D / 2); ` ` ` ` ` `return` `N * power(N, D / 2) ` ` ` `* power(N, D / 2); ` `} ` ` ` `// Function to calculate the order of ` `// the number ` `int` `order(` `int` `N) ` `{ ` ` ` `int` `r = 0; ` ` ` ` ` `// For each digit ` ` ` `while` `(N) { ` ` ` `r++; ` ` ` `N = N / 10; ` ` ` `} ` ` ` `return` `r; ` `} ` ` ` `// Function to check whether the given ` `// number is Armstrong number or not ` `int` `isArmstrong(` `int` `N) ` `{ ` ` ` `// To find order of N ` ` ` `int` `D = order(N); ` ` ` ` ` `int` `temp = N, sum = 0; ` ` ` ` ` `// Traverse each digit ` ` ` `while` `(temp) { ` ` ` `int` `Ni = temp % 10; ` ` ` `sum += power(Ni, D); ` ` ` `temp = temp / 10; ` ` ` `} ` ` ` ` ` `// If satisfies Armstrong condition ` ` ` `if` `(sum == N) ` ` ` `return` `1; ` ` ` `else` ` ` `return` `0; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given Number N ` ` ` `int` `N = 153; ` ` ` ` ` `// Function Call ` ` ` `if` `(isArmstrong(N) == 1) ` ` ` `cout << ` `"True"` `; ` ` ` `else` ` ` `cout << ` `"False"` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

True

**Time Complexity:** *O(log _{10}N)*

**Auxiliary Space:**

*O(1)*

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Program for Armstrong Numbers
- Armstrong Numbers between two integers
- XOR and OR of all N-digit Armstrong numbers
- Sum of all armstrong numbers lying in the range [L, R] for Q queries
- Range Queries for count of Armstrong numbers in subarray using MO's algorithm
- C Program to check Armstrong Number
- TCS Coding Practice Question | Check Armstrong Number
- Array Range Queries to find the Maximum Armstrong number with updates
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Count numbers which can be constructed using two numbers
- Maximum sum of distinct numbers such that LCM of these numbers is N
- Permutation of numbers such that sum of two consecutive numbers is a perfect square
- Numbers within a range that can be expressed as power of two numbers
- Numbers less than N which are product of exactly two distinct prime numbers
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Count numbers which are divisible by all the numbers from 2 to 10
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Check if a given pair of Numbers are Betrothed numbers or not

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.