C/C++ program for Armstrong Numbers - GeeksforGeeks

11 Jul, 2020
Given a number N, the task is to check whether the given number is Armstrong number or not. If the given number is Armstrong Number then print “Yes” else print “No”.
A positive integer of D digits is called an armstrong-numbers of order D (order is the number of digits) ifWhere D is number of digit in number Nand N(1), N(2), N(3)... are digit of number N.
Examples:
Input: N = 153Output: YesExplanation:153 is an Armstrong number.1*1*1 + 5*5*5 + 3*3*3 = 153
Input: 120Output: NoExplanation:120 is not an Armstrong number.1*1*1 + 2*2*2 + 0*0*0 = 9
Approach: The idea is to count the number of digits(say d) in the given number N. For every digit(say r) in the given number N find the value of rd and if the summation of all the values is N then print “Yes” else print “No”.
Below is the implementation of the above approach:
C
C++
// C program to find Armstrong number#include <stdio.h>  // Function to calculate N raised// to the power Dint power(int N, unsigned int D){    if (D == 0)        return 1;      if (D % 2 == 0)        return power(N, D / 2)               * power(N, D / 2);      return N * power(N, D / 2)           * power(N, D / 2);}  // Function to calculate the order of// the numberint order(int N){    int r = 0;      // For each digit    while (N) {        r++;        N = N / 10;    }    return r;}  // Function to check whether the given// number is Armstrong number or notint isArmstrong(int N){    // Calling order function    int D = order(N);      int temp = N, sum = 0;      // For each digit    while (temp) {        int Ni = temp % 10;        sum += power(Ni, D);        temp = temp / 10;    }      // If satisfies Armstrong condition    if (sum == N)        return 1;    else        return 0;}  // Driver Codeint main(){    // Given Number N    int N = 153;      // Function Call    if (isArmstrong(N) == 1)        printf("True\n");    else        printf("False\n");    return 0;}
// C++ program to find Armstrong number#include <bits/stdc++.h>using namespace std;  // Function to calculate N raised// to the power Dint power(int N, unsigned int D){    if (D == 0)        return 1;      if (D % 2 == 0)        return power(N, D / 2)               * power(N, D / 2);      return N * power(N, D / 2)           * power(N, D / 2);}  // Function to calculate the order of// the numberint order(int N){    int r = 0;      // For each digit    while (N) {        r++;        N = N / 10;    }    return r;}  // Function to check whether the given// number is Armstrong number or notint isArmstrong(int N){    // To find order of N    int D = order(N);      int temp = N, sum = 0;      // Traverse each digit    while (temp) {        int Ni = temp % 10;        sum += power(Ni, D);        temp = temp / 10;    }      // If satisfies Armstrong condition    if (sum == N)        return 1;    else        return 0;}  // Driver Codeint main(){    // Given Number N    int N = 153;      // Function Call    if (isArmstrong(N) == 1)        cout << "True";    else        cout << "False";    return 0;}
True

Time Complexity: O(log10N)Auxiliary Space: O(1)
number-digits
number-theory
Numbers
C Programs
C++ Programs
Mathematical
number-theory
Mathematical
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C Program to read contents of Whole File
Header files in C/C++ and its uses
How to return multiple values from a function in C or C++?
How to Append a Character to a String in C
Program to print ASCII Value of a character
Header files in C/C++ and its uses
How to return multiple values from a function in C or C++?
C++ Program for QuickSort
Program to print ASCII Value of a character
Sorting a Map by value in C++ STL