Last Updated on August 18, 2023 by Mayank Dham
Armstrong numbers, also known as narcissistic numbers or pluperfect digital invariant numbers, are a fascinating mathematical concept that holds significance in the world of programming. An Armstrong number is a number that is equal to the sum of its own digits, each raised to the power of the number of digits. In this article, we’ll explore how to identify Armstrong numbers using a for loop in the C programming language. We’ll also discuss an alternative method to find Armstrong numbers without using a loop. By delving into this concept, you’ll gain insights into programming logic, loops, and mathematical properties.
What is the Armstrong Number?
A number called the Armstrong number is the sum of its digits’ cubes. The Armstrong numbers, for illustration, are 0, 1, 153, 370, 371, and 407.
Before continuing with this article, we recommend that you should learn these C programming topics:
Armstrong Number
A number called the Armstrong number is the sum of its digits’ cubes. The Armstrong numbers, for illustration, are 0, 1, 153, 370, 371, and 407.
Before continuing with this article, we recommend that you should learn these C programming topics:
- For loop in C
- If statement in C
- Do while in C
Logic Used for Armstrong
Consider the number 407; it has three digits, thus it can be written as:
(407) —> (444) + (000) + (777)
64 + 0 + 343
=> 407
407 is an Armstrong number because it is the sum of the cubes of its digits.
Explanation with Example
Let input is equal to 153
variable values before for loop
num=153; check=153; Armstrong=0; result=0;
values of the variable in for loop are line by line
for i=1
Armstrong=3;
num=15;
Armstrong=333=27
result=0+27=27;
for loop condition check: num is not equal to zero loops will run again
for i=2
Armstrong=5;
num=1;
Armstrong=555=125
result=27+125=152;
for loop condition: num is not equal to zero loops will run again
for i=3
Armstrong=1;
num=0;
Armstrong=111=1;
result=152+1=153;
In for loop condition, the num is EQUAL TO ZERO therefore, the loop will run again
The loop will break and if else condition will be checked as or result=153 and check=153
if the condition will true program will show the output
153 is an Armstrong Number
Armstrong Number Algorithm:
- Start
- Read an integer input number.
- Declare and initialize the variables current_digit , sum = 0 and num = number.
- Repeat Steps 5 to 7 Until num > 0.
- current_digit = (num % 10) %10 return the last digit of the current number
- sum = sum + (current_digit current_digit current_digit).
- The variable current_digit is multiplied three times because we check for a three-digit Armstrong number.
- num = num / 10
After processing the last digit, we need to remove it. /10 will give an integer such that the order of the number will be reduced and the next digit will become the last digit.
- Check if sum == number. Then Print “It is an Armstrong Number.” Else Print “It is not an Armstrong Number.”
C Program Code to Check Armstrong Number
Method 1: In this method, we will take a three-digit number let’s say 371 and we will check if 371 is an Armstrong number or not.
#include<stdio.h> #include<math.h> int isArmstrong(int number) { int current_digit, sum = 0, num = number, number_of_digits; while (num > 0) { current_digit = num % 10; sum = sum + pow(current_digit, 3); num = num / 10; } if (sum == number) { return 1; } else { return 0; } } int main() { int number = 371, isArmstrongNumber; isArmstrongNumber = isArmstrong(number); if (isArmstrongNumber == 1) { printf("%d is an Armstrong Number.", number); } else { printf("%d is not an Armstrong Number.", number); } return 0; }
Method 2: In this method, we will use different functions for checking if the given number is an Armstrong number or not.
#include <stdio.h> #include <math.h> int countNumberOfDigits(int number, int number_of_digits) { while (number > 0) { number = number / 10; number_of_digits++; } return number_of_digits; } int isArmstrong(int number) { int current_digit, sum = 0, num = number, number_of_digits = 0; number_of_digits = countNumberOfDigits(num, number_of_digits); while (num > 0) { current_digit = num % 10; sum = sum + pow(current_digit, number_of_digits); num = num / 10; } if (sum == number) return 1; else return 0; } int main() { int number, isArmstrongNumber; scanf("%d", & number); isArmstrongNumber = isArmstrong(number); if (isArmstrongNumber == 1) printf("%d is an Armstrong Number.", number); else printf("%d is not an Armstrong Number.", number); return 0; }
Time complexity: Time complexity will be O(X), where X = log10(N), and N is the number we are determining to be an Armstrong number because we are evaluating each digit of the number.
Space complexity: In this, we are not using any extra space, therefore the space complexity is O(1).
Bonus Method: Armstrong Number in C Without Loop
Instead of using loops, we can employ recursion to identify Armstrong numbers. The idea is to extract the last digit of the number, raise it to the power of the number of digits, and then recursively apply the same process to the remaining digits. This approach eliminates the need for explicit loops and offers an elegant solution to the problem.
Code Implementation For Armstrong Number in C Without Loop
#include <stdio.h> #include <math.h> int isArmstrong(int num, int power); int main() { int number; printf("Enter an integer: "); scanf("%d", &number); int numDigits = (int)log10(number) + 1; if (isArmstrong(number, numDigits) == number) { printf("%d is an Armstrong number.\n", number); } else { printf("%d is not an Armstrong number.\n", number); } return 0; } int isArmstrong(int num, int power) { if (num == 0) { return 0; } int digit = num % 10; return pow(digit, power) + isArmstrong(num / 10, power); }
Time Complexity: Since the function is called n times, and each function call involves constant-time operations, the overall time complexity is O(n) for Armstrong Number in C Without Loop, where n is the number of digits in the input number.
Space complexity: Since the memory used for local variables remains constant throughout the execution, regardless of the input size, the space complexity of this algorithm is O(1) for Armstrong Number in C Without Loop, indicating constant space usage.
Tip: Many companies like Wipro, TCS, Accenture, and Infosys ask these types of questions in their hiring process to check the basic logic of the candidates.
Conclusion
In this exploration of Armstrong numbers in C, we’ve delved into two distinct methods to identify these intriguing mathematical entities. The first method, without using loops, leverages the power of recursion to break down the problem into elegant recursive calls. By utilizing this approach, we showcased the beauty of mathematical patterns and the versatility of programming techniques. On the other hand, the second method employs a traditional for loop to iteratively analyze the digits of a number, presenting a more straightforward approach to the problem.
Through these methods, you’ve gained a comprehensive understanding of Armstrong numbers, delved into recursion as an alternative to loops, and observed how the same problem can be solved using different strategies. Armed with this knowledge, you’re better equipped to tackle similar challenges and make informed decisions about the most appropriate approach for various scenarios.
FAQs for Armstrong Number in C Without Loop
Here are some FAQs for Armstrong Number in C using For Loop.
Q1: What is an Armstrong number?
A: An Armstrong number is an n-digit number that equals the sum of its own digits each raised to the power of n.
Q2: Why use recursion instead of loops to find Armstrong numbers?
A: Recursion provides an elegant way to solve problems by breaking them down into smaller subproblems. It showcases the mathematical essence of Armstrong numbers and highlights the power of recursive programming techniques.
Q3: How does the recursion-based method work?
A: The recursive approach extracts the last digit of the number, raises it to the power of the number of digits, and recursively applies the same process to the remaining digits. The sum of these calculations is then compared to the original number to determine if it’s an Armstrong number.
Other C Programs
C Program for Binary Search
C Program to Add Two Numbers
C Program to Calculate Percentage of 5 Subjects
C Program to Convert Binary Number to Decimal Number
C Program to Convert Celsius to Fahrenheit
C Program to Convert Infix to Postfix
C Program to Find Area of Circle
C Program to Find Roots of Quadratic Equation
C program to Reverse a Linked List
C program to reverse a number
Ascending Order Program in C
Menu Driven Program For All Operations On Doubly Linked List in C
C Program For Merge Sort For Linked Lists
C program for performing Bubble sort on Linked List
Hello World Program in C
Perfect Number Program in C
Leap Year Program in C
Odd Even Program in C
Selection Sort Program in C