Armstrong Numbers in C offers an interesting opportunity to get into number properties. By grasping the algorithm and putting it into code, you can ascertain if a given number qualifies as an Armstrong Number. This article demonstrates how to check for Armstrong Numbers in a C program, outlining two approaches: one tailored for 3-digit numbers and the other adaptable for any N-digit number.

What is an Armstrong Number?

An Armstrong Number is one where each digit, raised to the power of its position and summed together, equals the original number. For example, a three-digit number XYZ would be an Armstrong Number if XYZ equals the sum of each digit raised to the power of three:

xyz...n = x^n + y^n + z^n

Get curriculum highlights, career paths, industry insights and accelerate your technology journey.

Download brochure

Examples of Armstrong Number

The below-given examples below will help you understand what an Armstrong Number is in a clear manner:

Example 1:

Input: 407

Output: yes

Explanation: 407 qualifies as an Armstrong Number among three-digit numbers, as it exhibits the property where the sum of the cubes of its individual digits equals the number itself.

4*4*4+0*0*0+7*7*7= 407

Example 2:

Input: 409

Output: No

409 is not an Armstrong Number.

Explanation: 409 doesn’t meet the criteria of an Armstrong Number within the three-digit range. The sum of the cubes of its digits equals 793; the number itself is 409.

4*4*4 + 0*0*0 + 9*9*9 = 793

Example 3:

Input: 1634

Output: Yes

Explanation: 1634 is an Armstrong Number among 4-digit numbers, as it exhibits the property where the sum of the 4 to the power of each of its individual digits equals the number itself.

1*1*1*1+ 6*6*6*6 + 3*3*3*3 + 4*4*4*4 = 1634

Three Digit Armstrong Number in C Program

Let’s examine the following example program aimed at determining whether a given three-digit number qualifies as an Armstrong Number in C or not. But, before that, look into the algorithm for a 3-digit number.

Algorithm for a 3-digit Number

Follow these steps:

Choose an integer input from the user (num).

Initialise sum to 0.

Assign the user input to a temporary variable (var = num).

Determine the total number of digits in the input number.

Execute steps 6 to 8 iteratively until var > 0.

Calculate the rightmost digit using the modulo operator (operating_digit = var % 10).

Add the cube of the operating_digit to sum (sum = sum + operating_digit * operating_digit * operating_digit).

Remove the rightmost digit by dividing the number by 10 (var = var / 10).

If the sum equals num, output “Entered number is an Armstrong Number.” Otherwise, output “Entered number is not an Armstrong Number.”

End.

In the initial step of the algorithm, an input number is accepted and stored in a temporary variable, with the sum initialised to 0. The subsequent step computes the total digits in the given number. The remainder of the input number is stored in the ‘operating digit’ variable, and its value is added to ‘num’ (the entered number). Note that ‘operating_digit’ is cubed since the algorithm targets a three-digit Armstrong Number in C.

Following this, the input number is divided by 10. To ascertain Armstrong Numbers in C using a for loop steps 6 to 8 iterate until ‘var’ > 0. For the Armstrong Number validation, the subsequent step compares the sum with the input number. If they match, the output confirms that the entered number is an Armstrong Number.

Here is an example program to see if a three-digit input is an Armstrong Number in C or not:

#include <stdio.h>
#include <math.h>
int Armstrong(int num);
int main() {
int num;
printf("Please enter a three-digit number: ");
scanf("%d", &num);
if (isArmstrong(num)) {
printf("%d is an Armstrong Number.n", num);
} else {
printf("%d is not an Armstrong Number.n", num);
}
return 0;
}
int isArmstrong(int num) {
int yourNum, remainder, result = 0;
yourNum = num;
// Calculate the Armstrong Number
while ( yourNum != 0) {
remainder = yourNum % 10;
result += pow(remainder, 3);
yourNum /= 10;
}
// Check if the calculated result matches the original number
if (result == num) {
return 1; // It is an Armstrong Number
} else {
return 0; // It is not an Armstrong Number
}
}

Output

Please enter a three-digit number: 153
153 is an Armstrong Number.

In the program provided above, we’ve employed a function called isArmstrong() to verify Armstrong Numbers in C. This function is used for three-digit numbers in our illustration. The program prompts the user to input a three-digit number, and then invokes the isArmstrong() function. Subsequently, it displays a message based on the outcome.

For the program that checks if a number is an Armstrong Number with three digits, the time complexity is O(log(n)). Here, ‘n’ is the input number given to the isArmstrong function. This is because the while loop runs logarithmically based on the number of digits in ‘n’.

Space Complexity:

The algorithm’s space complexity is O(1), indicating it needs a constant amount of extra memory. The memory usage doesn’t change with the input number’s size.

N-Digit Armstrong Number in C Program

To verify Armstrong Numbers in C for N-digit numbers, we can adapt a comparable method to the one used for 3-digit numbers. By adjusting the program structure to handle varying numbers of digits, we enable flexibility. Incorporating functions enhances the readability and facilitates easier debugging of the Armstrong Number code.

Take an integer input from the user, storing it in the variable “num”.

Set “sum” to zero and assign the value of “num” to a temporary variable.

Determine the total count of digits in the input number and store this count in the variable “total_of_digits”.

Continue executing steps 5 through 7 until “num” becomes greater than zero.

Extract the rightmost digit of “num” using the modulo operator and store it in “operating_digit”.

Add the result of raising “operating_digit” to the power of “total_of_digits” to the “sum”.

Update the value of “num” by dividing it by 10.

If the “sum” equals “num”, then print “Entered number is an Armstrong Number.”; otherwise, print “Entered number is not an Armstrong Number.”.

Initially, the user provides an input number. Then, the sum is set to zero, and the input number is stored. Afterwards, the total count of digits in the input is calculated and stored. Steps 5 through 7 continue until ‘num’ becomes greater than 0.

In step 5, the last digit of ‘num’ is isolated and saved. The sixth step computes the power of ‘operating_digit’ based on the ‘total_of_digits’, adding this to ‘sum.’ Step 7 adjusts ‘num’ by removing its last digit. Finally, in step 8, ‘sum’ and ‘num’ are compared. If they match, the entered number is confirmed as an Armstrong Number.

Below is a sample program demonstrating how to verify whether an input N-digit number qualifies as an Armstrong Number in C.

#include <stdio.h>
#include <math.h>
int isArmstrong(int num);
int main() {
int num;
printf(“Please enter a number: “);
scanf(“%d”, &num);
if (isArmstrong(num)) {
printf(“%d is an Armstrong Number.n”, num);
} else {
printf(“%d is not an Armstrong Number.n”, num);
}
return 0;
}
int isArmstrong(int num) {
int yourNum, remainder, n = 0, result = 0;
yourNum = num;
// Count the number of digits
while ( yourNum != 0) {
yourNum /= 10;
++n;
}
yourNum = num;
// Calculate the Armstrong Number
while ( yourNum != 0) {
remainder = yourNum % 10;
result += pow(remainder, n);
yourNum /= 10;
}
// Check if the calculated result matches the original number
if (result == num) {
return 1; // It is an Armstrong Number
} else {
return 0; // It is not an Armstrong Number
}
}

The mentioned program underscores the concept of Armstrong Numbers in C through function utilisation. It employs the ‘isArmstrong()’ function to determine the digit count of the input number. This function calculates the sum of the powers of individual digits and verifies whether it corresponds to the original number (referred to as ‘yourNum’ in our example). The `main()` function prompts the user to input a number and invokes the `isArmstrong()` function accordingly. Ultimately, it displays the resulting output.

Here’s the output you would observe upon executing the above program and providing an N-digit number:

Output:

Please input a number: 123
123 is not an Armstrong Number.

Analysis of Algorithm

You may evaluate the aforementioned algorithm considering its time complexity and space complexity.

Time Complexity:

The time complexity is O(X) as the algorithm iterates through the individual digits of the number. Here, X = log10(N), where N represents the number being examined for Armstrong property.

Space Complexity:

In terms of space complexity, the algorithm maintains O(1) space requirements since it doesn’t necessitate any additional space that scales with the size of the input.

Winding Up

Understanding Armstrong Numbers provides a fascinating insight into the relationship between digits within a number and their powers. By examining examples and exploring algorithms, we’ve illustrated how to identify Armstrong Numbers, both for three-digit and N-digit cases, using C programming. Through these discussions, we’ve uncovered the underlying principles and computational methods essential for recognizing these special numbers.

Moreover, by analysing the time and space complexities of the algorithms, we’ve gained a deeper understanding of their efficiency and scalability. Armstrong Numbers stands as a captivating concept bridging mathematics and computer science, showcasing the elegance and practicality of numerical exploration in programming. Join the Certificate Program in Full Stack Development with Specialization for Web and Mobile to understand it better.

FAQs

Can you check for Armstrong Number for negative or decimal numbers?

NO. Only positive integers are defined under Armstrong Number.

How to check Armstrong Number for a large number?

The approach remains consistent with smaller numbers, yet for larger ones, employ a larger data type such as 'long' or 'Big Integer' to accommodate the increased size of numbers and the resultant cubing and summation.

What is the time complexity of the Armstrong Number algorithm?

The time complexity of the Armstrong Number algorithm is O(n), where 'n' represents the number of digits in the input number. This is because each digit of the number must be iterated through to compute the sum of the digits raised to the power of the total number of digits.

Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.