armstrong_number.cpp File Reference

Program to check if a number is an Armstrong/Narcissistic number in decimal system. More...

`#include <cassert>`
`#include <cmath>`
`#include <iostream>`
Include dependency graph for armstrong_number.cpp:

## Functions

int | number_of_digits (int num)
bool | is_armstrong (int number)
void | test ()
int | main ()

## Detailed Description

Program to check if a number is an Armstrong/Narcissistic number in decimal system.

Armstrong number or Narcissistic number is a number that is the sum of its own digits raised to the power of the number of digits.

Author
  iamnambiar

## Function Documentation

## is_armstrong()

bool is_armstrong | ( | int | number | ) |

Function to check whether the number is armstrong number or not.

Parameters

  num | Number

Returns
  `true` if the number is armstrong.
  `false` if the number is not armstrong.

{
// If the number is less than 0, then it is not a armstrong number.
if (number < 0) {
return false;
}
int sum = 0;
int temp = number;
// Finding the total number of digits in the number
int total_digits = number_of_digits(number);
while (temp > 0) {
int rem = temp % 10;
// Finding each digit raised to the power total digit and add it to the
// total sum
sum = sum + std::pow(rem, total_digits);
temp = temp / 10;
}
return number == sum;
}
number_of_digits
int number_of_digits(int num)
Definition: armstrong_number.cpp:21
machine_learning::sum
T sum(const std::vector< std::valarray< T >> &A)
Definition: vector_ops.hpp:232
std::pow
T pow(T... args)
Here is the call graph for this function:

## main()

int main | ( | void |  | ) |

Main Function

{
test();
return 0;
}
test
void test()
Definition: armstrong_number.cpp:59
Here is the call graph for this function:

## number_of_digits()

int number_of_digits | ( | int | num | ) |

Function to calculate the total number of digits in the number.

Parameters

  num | Number

Returns
  Total number of digits.

{
int total_digits = 0;
while (num > 0) {
num = num / 10;
++total_digits;
}
return total_digits;
}

## test()

void test | ( |  | ) |

Function for testing the is_armstrong() function with all the test cases.

{
// is_armstrong(370) returns true.
assert(is_armstrong(370) == true);
// is_armstrong(225) returns false.
assert(is_armstrong(225) == false);
// is_armstrong(-23) returns false.
assert(is_armstrong(-23) == false);
// is_armstrong(153) returns true.
assert(is_armstrong(153) == true);
// is_armstrong(0) returns true.
assert(is_armstrong(0) == true);
// is_armstrong(12) returns false.
assert(is_armstrong(12) == false);
}
is_armstrong
bool is_armstrong(int number)
Definition: armstrong_number.cpp:36
Here is the call graph for this function: