Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
1.
Introduction
1.1.
Sample Examples
2.
Armstrong Number Using a For Loop
2.1.
Implementation in C++
2.1.1.
Complexity Analysis
3.
Armstrong Number Using a While Loop
3.1.
Implementation in C++
3.1.1.
Complexity Analysis
4.
Armstrong Number using Recursion
4.1.
Implementation in C++
4.1.1.
Complexity Analysis
5.
5.1.
What are Armstrong numbers 1 to 100?
5.2.
Why is 371 an Armstrong number?
6.
Conclusion
Last Updated: Aug 3, 2024
Easy

# Armstrong Number in C

## Introduction

This blog will discuss the solution to this problem of checking if a number is an Armstrong number or not. We will code the solution to this problem in C language. We will discuss three different approaches to solve this problem. But before discussing the different solutions to this problem, we should first look at what exactly an Armstrong number is and by whom it was found.

A number is called an Armstrong number if the sum of every digit present in that number raised to the number of digits is equal to that number. Armstrong's number, also known as the narcissistic number, was discovered by Michael Armstrong and was also named after him.

Here is a general representation of an Armstrong number,

### Sample Examples

Example 1:

``````Input:
N = 371

Output
371 is an Armstrong number.

Explanation
3*3*3 + 7*7*7 + 1*1*1 = 27 + 343 + 1 = 371
Since the sum of individual powers of every digit is equal to the number itself, therefore this number is an Armstrong number.``````

Example 2:

``````Input:
N = 8208
Output:
8208 is an Armstrong number

Explanation
8*8*8*8 + 2*2*2*2 + 0*0*0*0 + 8*8*8*8 = 4096 + 16 + 0 + 4096 = 8208
Since the sum of individual powers of every digit is equal to the number itself, therefore we can say that the given number is Armstrong's number.``````

You can also read about dynamic array in c and C Static Function.

Also Read - Strong number in c

## Armstrong Number Using a For Loop

1. First, we will calculate the number of digits in a number N using a while loop.
2. After that, we run a for loop from 0 to the number of digits of the number, and at each iteration, we will take the current digit and add the current digit raised to the power of the number of digits in the sum variable.
3. In the last, we will check if the sum value is equal to N. We will print that the given number N is an Armstrong number. Else we will print that the given number is not an Armstrong number.

### Implementation in C++

``````// C program to check if a number is Armstrong Number or not
#include <stdio.h>
#include <math.h>

int main()
{
int N, countOfDigits=0,  temp = 0;

scanf("%d", &N);

temp = N;

while (temp != 0) // to count the digits in the number
{
countOfDigits = countOfDigits + 1;
temp = temp / 10;
}

int r, sum = 0;

for (int i = N; i > 0; i = i / 10)
{
r = i % 10; // to store the digit
sum = sum + pow(r, countOfDigits); // to store the sum
}

if (N == sum)
printf("%d is an Armstrong Number.\n", N);
else
printf("%d is not an Armstrong Number.\n", N);

return 0;
}``````

Output:

``````Input: N = 1634
Output: 1634 is an Armstrong Number
1*1*1*1 + 6*6*6*6 + 3*3*3*3 + 4*4*4*4 = 1 + 1296 + 81 + 216 = 1634``````

#### Complexity Analysis

Time Complexity: O(log(N))

Since we are using a single for loop from 0 to the count of the digits, therefore, we can say that the complexity of the above approach is O(Count of digits of a Number) which is approximately equal to O(log(N)).

Space Complexity: O(1)

## Armstrong Number Using a While Loop

1. First, we will calculate the number of digits in a number N using a while loop.
2. After that, we will run a while loop till the number does not become zero (you can call it the same as running a for loop from 0 to the count of digits). At each iteration, we will take the current digit and add the current digit raised to the power of the number of digits in the sum variable.
3. In the last, we will check if the sum value is equal to N or not. If it is, we will print that N is an Armstrong number. Else we will print that the given number is not an Armstrong number.

### Implementation in C++

``````// C program to check if a number is Armstrong Number or not
#include <stdio.h>
#include <math.h>

int main()
{
int N, countOfDigits = 0, temp = 0;

scanf("%d", &N);

temp = N;

while (temp != 0) // to count the digits in the number
{
countOfDigits = countOfDigits + 1;
temp = temp / 10;
}

int r, sum = 0;
temp = N;

while (temp)
{
r = temp % 10; // to store the current digit
temp /= 10;
sum += pow(r, countOfDigits); // to store the sum
}

if (N == sum)
printf("%d is an Armstrong Number.\n", N);
else
printf("%d is not an Armstrong Number.\n", N);

return 0;
}``````

Output:

``````Input: N=370
Output: 370 is an Armstrong number
3*3*3 + 7*7*7 + 0*0*0 = 27 + 243 + 0 = 370``````

#### Complexity Analysis

Time Complexity: O(N(log(N))

Since we are using a single while loop from 0 to the count of the digits, therefore we can say that the complexity of the above approach is O(Count of digits of a Number) which is approximately equal to O(log(N)).

Space Complexity: O(1)

Also see, Tribonacci Series and Short int in C Programming

## Armstrong Number using Recursion

1. First, we will calculate the number of digits in a number N using a while loop.
2. After that, we call another function to make recursive calls to cover all the digits one by one; while iterating the digits, we will add the current digit raised to the power of the number of digits in the sum variable.
3. In the last, we will check if the sum value is equal to N. We will print that the given number N is an Armstrong number. Else we will print that the given number is not an Armstrong number.

### Implementation in C++

``````#include <stdio.h>
#include <math.h>

void ifArmstrong(int, int, int &);

int main()
{
int N;

scanf("%d", &N);

int temp = N, countOfDigits = 0;
while (temp != 0) // to count the digits in the number
{
countOfDigits = countOfDigits + 1;
temp = temp / 10;
}
int sum = 0;
ifArmstrong(N, countOfDigits, sum);
if (N == sum)
printf("%d is an Armstrong number.\n", N);
else
printf("%d is not an Armstrong number.\n", N);

return 0;
}

void ifArmstrong(int N, int countOfDigits, int &sum)
{
int r;

if (N > 0)
{
r = N % 10; // to store the current digit
sum = sum + pow(r, countOfDigits); // to store the sum
ifArmstrong(N / 10, countOfDigits, sum);
}
else
return;
}``````

Output:

``````Input: N=407

Output: 407 is an Armstrong number
4*4*4 + 0*0*0 + 7*7*7 = 64 + 0 + 343 = 407``````

#### Complexity Analysis

Time Complexity: O(log(N)

Since we are making recursive calls that are equal to the count of digits in the number, therefore, we can say that the complexity of the above approach is O(Count of digits of a Number) which is approximately equal to O(log(N)).

Space Complexity: O(1)

Check out this problem - Subarray Sum Divisible By K

Must Read Decision Making in C

## Frequently Asked Questions

### What are Armstrong numbers 1 to 100?

Armstrong numbers between 1 and 100 are 1, 2, 3, 4, 5, 6, 7, 8, 9, and 153. These numbers are equal to the sum of their digits each raised to the power of the number of digits.

### Why is 371 an Armstrong number?

371 is considered an Armstrong number since the sum of its digits each raised to the power of three (3^3, 7^3, 1^3) equals 371. Hence, it fits the Armstrong number definition.

## Conclusion

This article discussed the problem of checking if a number is an Armstrong number or not. We have discussed three approaches to this problem: the approach using a for loop, another approach using a while loop, and the last approach using Recursion. We have also discussed the time complexities of all the approaches.

I hope you must have gained some knowledge after reading this problem, and now it is your turn to code this problem of Armstrong Number

Until then, Keep Learning and Keep Coding.

Live masterclass