-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
58 lines (49 loc) · 1.43 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
Author: Max Kapsecker
Version: 1.0
This code snippet performs the 196-algorithm to find number palindromes.
*/
#include <iostream>
/*
This function takes a number as input and reverses it
params: integer k
return: reversed integer k
example: 12345 -> 54321
*/
int reverseNumber(int k){
int remainder, reversedNumber = 0;
while(k != 0){
remainder = k % 10;
reversedNumber = reversedNumber * 10 + remainder;
k /= 10;
}
return reversedNumber;
}
int main(){
int totalNumber, iterations, temp;
std::cout << "How many number do you want to check?" << std::endl;
std::cin >> totalNumber;
std::cout << "Number" << "\t" << "Iterations" << "\t" << "Final palindrome" << std::endl;
for(int i = 1; i <= totalNumber; ++i){
iterations = 0;
// Is the number already a palindrome (e.g. 111)
if(i == reverseNumber(i)){
std::cout << i << "\t" << iterations << "\t \t" << i << std::endl;
continue;
}
temp = i + reverseNumber(i);
iterations++;
// Iterate until we have foung a palindrome
while(temp != reverseNumber(temp)){
temp = temp + reverseNumber(temp);
iterations++;
}
// Print result
if(temp < 0){
std::cout << i << "\t" << "Could not find a solution that fit into the datatype int" << std::endl;
}else{
std::cout << i << "\t" << iterations << "\t \t" << temp << std::endl;
}
}
return 0;
}