top

Search

C Tutorial

.

UpGrad

C Tutorial

Armstrong Number in C

What is an Armstrong Number in C?

Armstrong Number in C refers to a number which is equal to the total sum of its individual digits, with each digit raised to the power of the total number of digits. In other words, a number can be termed an Armstrong number if the total sum of its comprising digits raised to the power of the number of digits returns the number.

Examples of three-digit Armstrong numbers are 0, 1, 153, 370, 371, and 407.

Examples of four-digit Armstrong numbers are 1634, 8208,  and 9474.

When considering a three-digit number, you can determine whether a number is an Armstrong number or not by calculating the sum of the cubes of its individual digits. If the sum returns the same as the original number, it is called an Armstrong number.

Suppose the Armstrong number is a positive integer of order n then it is defined as,

x1x2x3x4…. = pow (x1, n) + pow (x2, n) + pow (x3, n) + pow (x4, n) + ………

Let’s take an example to understand Armstrong number in C:

Suppose the given number is 153. It is a three-digit number so the length of power is 3. To check whether it is an Armstrong number or not, we will add the cubes of individual digits.

13 = (1*1*1)=1 

53 = (5*5*5)=125 

33 = (3*3*3)=27  

So: 

1+125+27=153  

153 = (1*1*1)+(5*5*5)+(3*3*3)

Hence, 153 is an Armstrong number.

Algorithm (for a 3-digit Number)

Step-1: Take an integer number as input from the user (num)

Step-2: Initialise sum = 0

Step-3: Store the user input in a temporary variable (var = num)

Step-4: Calculate the total number of digits in the input number

Step-5: Repeat steps 6 to 8 until var > 0

Step-6: Calculate the rightmost digit using the modulo operator (operating_digit = var % 10)

Step-7: Add the cube of the operating_digit to the sum (sum = sum + operating_digit * operating_digit * operating_digit)

 Step-8: Divide the number by 10 to remove the rightmost digit (var = var / 10)

Step-9: If sum == num, print “Entered number is an Armstrong Number.” Else print “Entered number is not an Armstrong Number.”

Step-10: End

In the first step of the above algorithm, a number is accepted as an input. Its value is stored in a temporary variable, and the sum is initialised to 0. The next step calculates the total number of digits in the given number. The remainder of the entered number is stored in the ‘operating digit’ variable. Its value is added to the ‘num’ (entered number). Note that the variable operating_digit is multiplied thrice since we are looking for a three-digit Armstrong Number in C.

In the next step, the input number is divided by 10. To check the Armstrong number in C using for loop, step 6 to 8 iterate until the ‘var’ >0.  For the Armstrong number check, the next step compares the sum to the input number. If both are equal,  the output displays that the entered number is an Armstrong Number.

Flowchart

Armstrong Number Program in C for Three-Digit Number

Let’s consider the below example program to check whether an input three-digit number is an Armstrong Number in C or not.

#include <stdio.h>
#include <math.h>
 
int isArmstrong(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 above program, we have checked for the Armstrong number in C using function, i.e. isArmstrong() function. It is used for three-digit numbers in our example. The above program prompts the user to enter a three-digit number and then calls the isArmstrong() function. Finally, it prints the message according to the result. 

Analysis of Algorithm

The algorithm can be analysed in terms of time complexity and space complexity.

 Time Complexity:

The time complexity of the above program that checks Armstrong number for a 3-digit code is O(log(n)). Here n is the input number to the isArmstrong function. The reason is that the while loop logarithmically runs according to the number of digits within n.

Space Complexity:

The space complexity of the algorithm is O(1), which means it requires constant additional memory. The memory usage does not depend on the size of the input number.

Algorithm (for N-digit Number)

Step-1: Take an integer number as input from the user (num)

Step-2: Initialise sum = 0, and num = number.

Step-3: Calculate the total number of digits in the input number. Store its value in the variable total_of_digits.

Step-4: Repeat steps 5 to 7 till num > 0

Step-5: operating_digit = (num % 10)

Step-6: sum = sum + pow(operating_digit,total_of_digits)

Step-7: num = num / 10

Step-8: If sum == num then print “Entered number is an Armstrong Number.” Else print “Entered number is not an Armstrong Number.”

Firstly, an input number is accepted by the user. The second step initialises the ‘sum’ variable to 0 and stores the input number in the ‘num’ variable. The third step calculates the sum of the number of digits in the input and stores its value in the ‘total_of_digits’ variable. Steps 5 to 7 iterate till num > 0.

The fifth step stores the remainder of the input into the ‘operating_digit’ variable. In the sixth step, the value of total_of_digits is raised to the operating_digit to calculate the power. Its value is added to the ‘sum’ variable. The seventh step divides the input number by 10 and stores the value in the ‘num’ variable. The eighth step compares sum and num variables. If both are equal, the output shows that the entered number is an Armstrong number. 

Armstrong Number Program in C for N-Digit Number

We can check Armstrong numbers in C for N-digit numbers using a similar approach as for 3-digit numbers. The program structure is modified to accommodate the variable number of digits. With the use of function, the code for Armstrong number becomes more readable and easy to debug.

Here’s an example program to check whether an input N-digit number is an Armstrong Number in C or not.

#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 above program emphasises on the Armstrong number in C using function because it uses isArmstrong() function that calculates the number of digits in your input number. It calculates the sum of powers of individual digits and checks whether it matches the original number (i.e. yourNum in our example). The main() function prompts the user to enter a number and calls the isArmstrong() function. Lastly, it displays the output.

Here’s the output you get when you run the above program and enter an N-digit number.

Output

Please enter a number: 123
123 is not an Armstrong number.

Analysis of Algorithm

You can analyse the above algorithm in the context of time complexity and space complexity.

Time Complexity:

Time complexity is O(X) because the algorithm processes individual digits of the number. Here X = log10(N); N is the number to be checked for an Armstrong number.

Space Complexity:

The space complexity of the algorithm is O(1) because it does not require any additional space that grows with the input size. 

Conclusion

In conclusion, the concept of Armstrong numbers in C provides a fascinating way to explore the properties of numbers. By understanding the algorithm and implementing it in code, we can determine whether a given number is an Armstrong number or not. The program structure can be modified to handle both 3-digit and N-digit numbers, making it versatile and adaptable. The use of functions enhances code readability and simplifies the debugging process.

While getting familiar with the basics of Armstrong number in C, check out upGrad’s Full Stack Software Development Bootcamp, which will fuel your career advancement in the tech industry. Covering demanding topics taught by industry experts, upGrad helps you to escalate your growth in STEM!

Enroll now to begin your journey! 

FAQs

Q. What is the significance of the Armstrong number?

Armstrong number is widely used for encryption and decryption to improve the strength of the algorithm. An Armstrong number’s length can be increased if higher security is demanded. 

Q. Is 123 an Armstrong number?

No, 123 is not an Armstrong number because 13 + 23 + 33 = 36, which is not equal to the number 123. 

Q. Is an Armstrong number an N digit?

An Armstrong number is an n-digit number because it equates to the total of the nth power of each of its digits. For example, 407 is an Armstrong number because N = 3 (number of digits), so 43 + 03 + 73 = 64 + 0 + 343 = 407 itself.

Q. Can an Armstrong number be negative?

No, because an Armstrong number is a digit which is the total of its digits raised to the third power equals the number itself. Since the number is equated with summation (which can never be negative), an Armstrong number can’t be negative. It will return false for negative numbers.

Leave a Reply

Your email address will not be published. Required fields are marked *