aboutsummaryrefslogtreecommitdiff
path: root/challenge-153/ulrich-rieke/cpp/ch-2.cpp
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-02-21 20:24:43 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-02-21 20:24:43 +0000
commit67cae0ac5fd06c1bbb42b64a7682889e339c9676 (patch)
tree3a6e05333a6cf757082f386c56736c664412f96a /challenge-153/ulrich-rieke/cpp/ch-2.cpp
parent9a09829259c888940dc5f171348777b34dd91cd3 (diff)
downloadperlweeklychallenge-club-67cae0ac5fd06c1bbb42b64a7682889e339c9676.tar.gz
perlweeklychallenge-club-67cae0ac5fd06c1bbb42b64a7682889e339c9676.tar.bz2
perlweeklychallenge-club-67cae0ac5fd06c1bbb42b64a7682889e339c9676.zip
- Added solutions by Ulrich Rieke.
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 ;
+}