Last active
November 7, 2018 15:56
-
-
Save lzzy12/0661aa0fe8127a5d009b848d9753b822 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int reverse(int in) { | |
// Takes an int | |
// @return reverse of a given number of int data type | |
int r = 0; | |
while (in != 0) | |
{ | |
r = (r * 10) + (in % 10); | |
in /= 10; | |
} | |
return r; | |
} | |
int joinNums(int num1, int num2) { | |
num2 = reverse(num2); | |
while (num2 > 0) { | |
num1 = num1 * 10 + num2 % 10; | |
num2 /= 10; | |
} | |
return num1; | |
} | |
int removeDigitAt(int i, int input) { | |
// Removes the digit at i place from input. eg. removeDigitAt(0, 25) will return 2 | |
int r = 0, factor = 10; | |
for (int j = 0; j < i; j++) { | |
factor *= 10; | |
} | |
int leftPart = input / factor; | |
int rightPart = input % (factor/10); | |
return joinNums(leftPart, rightPart); | |
} | |
int digitCount(int in) { | |
//Takes an int | |
// @return the count of digits in a number.. eg. if in = 56674, it returns 5 | |
int count = 0; | |
while (in != 0) { | |
count++; | |
in /= 10; | |
} | |
return count; | |
} | |
int main() | |
{ | |
int input, n; | |
cin >> n; | |
for (int j = 1; j <= n; j++) | |
{ | |
cout << "Enter the number: " << endl; | |
cin >> input; | |
int greatest = removeDigitAt(0, input); | |
for (int i = 0; i < digitCount(input); i++) | |
{ | |
if (removeDigitAt(i, input) > greatest) | |
{ | |
greatest = removeDigitAt(i, input); | |
} | |
} | |
cout << greatest << endl; | |
} | |
cin.ignore(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment