aboutsummaryrefslogtreecommitdiff
path: root/challenge-153/ulrich-rieke/cpp/ch-2.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-153/ulrich-rieke/cpp/ch-2.cpp')
-rw-r--r--challenge-153/ulrich-rieke/cpp/ch-2.cpp34
1 files changed, 34 insertions, 0 deletions
diff --git a/challenge-153/ulrich-rieke/cpp/ch-2.cpp b/challenge-153/ulrich-rieke/cpp/ch-2.cpp
new file mode 100644
index 0000000000..51fd65fda0
--- /dev/null
+++ b/challenge-153/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,34 @@
+#include <iostream>
+#include <cstdlib>
+#include <vector>
+#include <algorithm>
+#include <numeric>
+
+int factorial( int num ) {
+ if ( num == 0 ) {
+ return 1 ;
+ }
+ else {
+ int fac = 1 ;
+ for ( int i = 1 ; i < num + 1 ; i++ )
+ fac *= i ;
+ return fac ;
+ }
+}
+
+int main( int argc, char* argv[] ) {
+ int n = std::atoi( argv[1] ) ;
+ int c = n ; //we are "destroying" n in the next lines
+ std::vector<int> digits ;
+ while ( n != 0 ) {
+ digits.push_back( n % 10 ) ;
+ n /= 10 ;
+ }
+ std::transform( digits.begin( ) , digits.end( ) , digits.begin( ) ,
+ factorial ) ;
+ if ( std::accumulate( digits.begin( ) , digits.end( ) , 0 ) == c )
+ std::cout << 1 << std::endl ;
+ else
+ std::cout << 0 << std::endl ;
+ return 0 ;
+}