C Program for Armstrong Number of Three Digits

We can simply calculate the sum of individual digits by dividing the number by 10 and getting reminders. And if it will be equal to the number itself then it is an Armstrong number. Here time complexity will be reduced to O(log(n)).

C




// C program to check given number is
// Armstrong number or not using Sum
// of Digits
#include <stdio.h>
 
// Driver code
int main()
{
    int n = 153;
    int temp = n;
    int p = 0;
 
    // Calculating the sum of individual digits
    while (n > 0) {
        int rem = n % 10;
        p = (p) + (rem * rem * rem);
        n = n / 10;
    }
 
    // Condition to check whether the
    // value of P equals to user input
    // or not.
    if (temp == p) {
        printf("Yes. It is Armstrong No.");
    }
    else {
        printf("No. It is not an Armstrong No.");
    }
    return 0;
}


Output:

Yes. It is Armstrong No.
  • Time Complexity: O(logn)
  • Auxiliary Space: O(1)

C Program to Check Armstrong Number

In this article, we will see how to check Armstrong number in the c program. We can implement two logic for checking Armstrong numbers, one for 3-digit numbers and the second one for more than 3 digits.

Similar Reads

What is Armstrong Number

The Armstrong number can be defined as a number that is equal to the result of the summation of the nth power of each n digit. Let’s assume we have a number XYZ  which is 3 digit number, so XYZ can be an Armstrong number if XYZ is equal to the result of the sum of each digit of the 3rd power....

C Program for Armstrong Number of Three Digits

We can simply calculate the sum of individual digits by dividing the number by 10 and getting reminders. And if it will be equal to the number itself then it is an Armstrong number. Here time complexity will be reduced to O(log(n))....

C Program for Armstrong Number of n Digits

...