From 088bd1a9bb76f41ca7a856e3268fe8ab71cdded4 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Wed, 3 Nov 2021 08:01:34 +0000 Subject: - Added solutions by Ulrich Rieke. --- challenge-137/ulrich-rieke/cpp/ch-2.cpp | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 challenge-137/ulrich-rieke/cpp/ch-2.cpp (limited to 'challenge-137/ulrich-rieke/cpp/ch-2.cpp') 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 +#include +#include +#include + +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 ; +} -- cgit