aboutsummaryrefslogtreecommitdiff
path: root/challenge-137/ulrich-rieke/cpp/ch-2.cpp
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-11-03 08:01:34 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-11-03 08:01:34 +0000
commit088bd1a9bb76f41ca7a856e3268fe8ab71cdded4 (patch)
tree1d9d03e41a0981e13291963f3f12780627a40fe3 /challenge-137/ulrich-rieke/cpp/ch-2.cpp
parent1cd169b85c4bcb26ecf76fb13fe1c356c8841ac6 (diff)
downloadperlweeklychallenge-club-088bd1a9bb76f41ca7a856e3268fe8ab71cdded4.tar.gz
perlweeklychallenge-club-088bd1a9bb76f41ca7a856e3268fe8ab71cdded4.tar.bz2
perlweeklychallenge-club-088bd1a9bb76f41ca7a856e3268fe8ab71cdded4.zip
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-137/ulrich-rieke/cpp/ch-2.cpp')
-rw-r--r--challenge-137/ulrich-rieke/cpp/ch-2.cpp40
1 files changed, 40 insertions, 0 deletions
diff --git a/challenge-137/ulrich-rieke/cpp/ch-2.cpp b/challenge-137/ulrich-rieke/cpp/ch-2.cpp
new file mode 100644
index 0000000000..8b254c8dcb
--- /dev/null
+++ b/challenge-137/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,40 @@
+#include <iostream>
+#include <string>
+#include <cstdlib>
+#include <algorithm>
+
+bool isPalindrome( int d ) {
+ std::string numberstring( std::to_string( d ) ) ;
+ std::string reversed { numberstring } ;
+ std::reverse( reversed.begin( ) , reversed.end( ) ) ;
+ return reversed == numberstring ;
+}
+
+int main( int argc, char * argv[] ) {
+ int n = std::atoi( argv[ 1 ] ) ;
+ int output = 1 ;
+ while ( n < 10 || n > 1000 ) {
+ std::cout << "number should be between 10 and 1000!" << std::endl ;
+ std::cin >> n ;
+ }
+ int iterations = 0 ;
+ int number = n ;
+ while ( iterations < 500 ) {
+ std::string numberstring( std::to_string( n ) ) ;
+ std::reverse( numberstring.begin( ) , numberstring.end( ) ) ;
+ number += std::stoi( numberstring ) ;
+ if ( isPalindrome( number ) ) {
+ output = 0 ;
+ break ;
+ }
+ if ( number > 10000000 ) {
+ output = 1 ;
+ break ;
+ }
+ iterations++ ;
+ }
+ if ( iterations >= 500 )
+ output = 1 ;
+ std::cout << output << std::endl ;
+ return 0 ;
+}