**Register Now!**

279 reads

by Rohan RaiMarch 1st, 2022

There are various categories of numbers on the basis of some set of rules like, if a number is divisible by two then it is an even number otherwise it will be an odd number. There could be more possible categories like prime number, composite number, twin prime number, and there are many more. Among these all there are special types of numbers known as Armstrong Numbers.

For a number, if the sum of all digits to their raised power equal to the length of the number is equal to the number itself then that number will be an armstrong number.

Let's take an example of 407:

-> Length of the 407 is 3 and if we take sum of all of its digit to raised power 3 that is **4*4*4 + 0*0*0 + 7*7*7 = 64 + 0 + 343 = 407**. As we get the result equal to **407**, which is our initial number that means **407** is an armstrong number.

**We will discuss two algorithms:**

- To find a given number is an armstrong number or not.
- Find the number of armstrong numbers between a given range.

**Steps for the algorithm:**

- Find the length of the given number using a while loop.
- Create a variable to store the number of digits in the given number.
- Run while loop until the current number becomes zero and keep dividing the current number by 10 and increasing the count variable to count the number of digits in the number.
- Now create a variable to store the sum of powers of each digit and initialize it with zero.
- Run a while loop until the current number becomes zero and keep dividing the current number by 10. In this process, the last digit of the current number can be obtained by using current_number % 10.
- Adding the last_digit raised to the power length of the number results in a variable that is created to store the total sum.
- At last, check if the total sum is equal to the given number then the given number is the Armstrong number otherwise not.

**Let’s take an example to implement the above given approach for better understanding:**

```
#include <math.h>
#include <stdio.h>
int main(){
// initial number
int n = 407;
// creating extra variable to store initial number
int temp_n = n;
// store the number of digits of n in count
int count = 0;
while(temp_n)
{
temp_n = temp_n / 10; // keep dividing by 10
count++;
}
// as temp_n becomes zero again assigning it n
temp_n = n;
// finding the required sum
int sum = 0;
while(temp_n)
{
// a contains the last digit
int a = temp_n % 10;
// store the sum of the power of individual digits in variable sum
sum += pow(a, count);
temp_n = temp_n / 10; // keep dividing
}
// if sum is equal to n, the number is an Armstrong number
if (sum == n)
{
printf("%d is an Armstrong number.", n);
}
else
{
printf("%d is not an Armstrong number.", n);
}
return 0;
}
```

**Output:**

```
407 is an Armstrong number
```

In the above approach, we keep dividing the current number by **10** until it becomes zero, that is **log10(N)** times. After that for every digit we raised their power to the number of digits, that is **log10(N)**.

So final complexity if **O(log10(N))2**.

In the above approach, we haven’t used extra space. So space complexity is **O(1)**.

**Steps for the algorithm:**

- First, we will implement a for loop in between the given range.
- We will create a function to check whether the current number is the Armstrong number or not.
- In the function, we will implement the approach which we have discussed above.
- At each iteration in for loop, we will simply pass the current number and if it is an Armstrong number then we will print it as an Armstrong number.
- Also, we are going to maintain the count of the total number of armstrong numbers.

**Lets see an example on the above given approach for better understanding:**

```
#include <math.h>
#include <stdio.h>
// function to check whether number is Armstrong or not
bool is_armstrong(int n)
{
// creating extra variable to store initial number
int temp_n = n;
// store the number of digits of n in count
int count = 0;
while(temp_n)
{
temp_n = temp_n / 10; // keep dividing by 10
count++;
}
// as temp_n becomes zero again assigning it n
temp_n = n;
// finding the required sum
int sum = 0;
while(temp_n)
{
// a contains the last digit
int a = temp_n % 10;
// store the sum of the power of individual digits in variable sum
sum += pow(a, count);
temp_n = temp_n / 10; // keep dividing
}
// if sum is equal to n, return true
if (sum == n)
{
return true;
}
else
{
return false;
}
}
int main(){
int start = 100, end = 1000;
printf("Print all Armstrong numbers between %d and %d:\n",start,end);
// for checking each number is Armstrong number or not, traversing a loop over them
// alos, we will maintain count of number of Armstrong number
int count = 0;
for(int i = start; i <= end;i++){
// calling function to check whether current number is Armstrong or not
if(is_armstrong(i))
{
// increase the count
count++;
printf("Armstrong number is:%d\n", i);
}
}
printf("Total %d number is an Armstrong number between 1 to 1000.", count );
return 0;
}
```

**Output:**

```
Print all Armstrong numbers between 100 and 1000:
Armstrong number is:370
Armstrong number is:371
Armstrong number is:407
Total 3 number is an Armstrong number between 1 to 1000.
```

In the above approach, we iterate over all of the numbers which take **O(N)** time where **N** is the number of elements in the range. Also, as we have seen in the above section, for number **M**, time complexity to check whether it is an armstrong number or not, takes **O(log10(M))2**.

So final complexity if **N*O(log10(N))2.**

In the above approach, we haven’t used extra space. So space complexity is **O(1)**.

- For a number, if the sum of all digits to their raised power equal to the length of the number is equal to the number itself then that number will be an armstrong number.
- Time complexity to find a number is an armstrong number or not is
**O(log10(N))2**. - Space complexity to find a number is armstrong or not is
**O(1)**. - Zero is also an Armstrong number.
- You may refer to this article by Scaler to cover all the concepts related to Armstrong numbers in c in detail.

L O A D I N G

. . . comments & more!

. . . comments & more!