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.
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
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
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.
Follow these steps:
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.
Also Read: Reversing a String in C: Comprehensive Guide with Multiple Approaches
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.
Also Read: Increment and Decrement Operators in C
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’.
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.
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.
Also Read: Binary Operators in C Programming
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.
You may evaluate the aforementioned algorithm considering its time complexity and space 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.
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.
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.
Book a free counselling session
Get a personalized career roadmap
Get tailored program recommendations
Explore industry trends and job opportunities
Programs tailored for your Success
Popular
Data Science
Technology
Finance
Management
Future Tech
© 2024 Hero Vired. All rights reserved