Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
armstrong_number.cpp File Reference

Checks whether a number is an Armstrong Number or not. More...

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

Namespaces

namespace  dynamic_programming
 Dynamic Programming algorithms.
 

Functions

template<typename T >
bool dynamic_programming::is_armstrong (const T &number)
 Checks if the given number is armstrong or not.
 
static void tests ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Checks whether a number is an Armstrong Number or not.

An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits. For example: 153 is an Armstrong number since 153 = 1^3 + 5^3 + 3^3.

A few examples of valid armstrong numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084.

Armstrong numbers are also known as Narcissistic Numbers, as stated in Wikipedia.

Author
Shivam Singhal
David Leal

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit
90 {
91 tests(); // run self-test implementations
92 return 0;
93}
static void tests()
Self-test implementations.
Definition armstrong_number.cpp:71
Here is the call graph for this function:

◆ tests()

static void tests ( )
static

Self-test implementations.

Returns
void
71 {
72 assert(dynamic_programming::is_armstrong(153) == true);
73 assert(dynamic_programming::is_armstrong(1) == true);
74 assert(dynamic_programming::is_armstrong(0) == true);
75 assert(dynamic_programming::is_armstrong(370) == true);
76 assert(dynamic_programming::is_armstrong(1634) == true);
77 assert(dynamic_programming::is_armstrong(580) == false);
78 assert(dynamic_programming::is_armstrong(15) == false);
79 assert(dynamic_programming::is_armstrong(1024) == false);
80 assert(dynamic_programming::is_armstrong(989) == false);
81 assert(dynamic_programming::is_armstrong(103) == false);
82
83 std::cout << "All tests have successfully passed!\n";
84}
bool is_armstrong(const T &number)
Checks if the given number is armstrong or not.
Definition armstrong_number.cpp:39
Here is the call graph for this function: