TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
armstrong_number_templated.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_templated.cpp:

Go to the source code of this file.

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

Definition in file armstrong_number_templated.cpp.

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 90 of file armstrong_number_templated.cpp.

90 {
91 tests(); // run self-test implementations
92 return 0;
93}
static void tests()
Self-test implementations.

◆ tests()

static void tests ( )
static

Self-test implementations.

Returns
void

Definition at line 71 of file armstrong_number_templated.cpp.

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.