C Program to check Armstrong Number

24 Jun, 2022
Given a number x, determine whether the given number is Armstrong number or not. 
A positive integer of n digits is called an Armstrong number of order n (order is number of digits) if. 
abcd... = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + .... 
Example: 
Input: 153
Output: Yes
153 is an Armstrong number.
1*1*1 + 5*5*5 + 3*3*3 = 153
Input: 120
Output: No
120 is not a Armstrong number.
1*1*1 + 2*2*2 + 0*0*0 = 9
Input: 1253
Output: No
1253 is not a Armstrong Number
1*1*1*1 + 2*2*2*2 + 5*5*5*5 + 3*3*3*3 = 723
Input: 1634
Output: Yes
1*1*1*1 + 6*6*6*6 + 3*3*3*3 + 4*4*4*4 = 1634
Approach: The idea is to first count number digits (or find order). Let the number of digits be n. For every digit r in input number x, compute rn. If sum of all such values is equal to n, then return true, else false.
C
// C program to find Armstrong number#include <stdio.h> // Function to calculate x// raised to the power yint power(int x, unsigned int y){    if (y == 0)        return 1;    if (y % 2 == 0)        return (power(x, y / 2) *                power(x, y / 2));    return (x * power(x, y / 2) *                power(x, y / 2));} // Function to calculate// order of the numberint order(int x){    int n = 0;    while (x)    {        n++;        x = x / 10;    }    return n;} // Function to check whether the// given number is Armstrong// number or notint isArmstrong(int x){    // Calling order function    int n = order(x);    int temp = x, sum = 0;    while (temp) {        int r = temp % 10;        sum += power(r, n);        temp = temp / 10;    }     // If satisfies Armstrong condition    if (sum == x)        return 1;    else        return 0;} // Driver Programint main(){    int x = 153;    if (isArmstrong(x) == 1)        printf("True\n");    else        printf("False\n");     x = 1253;    if (isArmstrong(x) == 1)        printf("True\n");    else        printf("False\n");     return 0;}
true
false
Time Complexity: O(logx*log(logx))
Auxiliary Space: O(1)
The above approach can also be implemented in a shorter way as:
C
// C program to implement// the above approach#include <stdio.h> // Driver codeint main(){        int n = 153;        int temp = n;        int p = 0;         // Function to calculate        // 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;} // This code is contributed by sathiyamoorthics19
Yes. It is Armstrong No.
Time Complexity: O(logn)
Auxiliary Space: O(1)
Please refer complete article on Program for Armstrong Numbers for more details!
jayanth_mkv
C Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C Program to read contents of Whole File
Producer Consumer Problem in C
Difference between break and continue statement in C
C Hello World Program
C program to find the length of a string
Exit codes in C/C++ with Examples
C / C++ Program for Dijkstra's shortest path algorithm | Greedy Algo-7
Handling multiple clients on server with multithreading using Socket Programming in C/C++
C Program to concatenate two strings without using strcat
Conditional wait and signal in multi-threading