Created
December 25, 2015 10:22
-
-
Save Deivur/1a563f4179ecb8c809de 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
public class Palindrome { | |
public static void main(String[] args) { | |
/* Initialization of variables, array; assigning input integer */ | |
int naturalNumber = 203; | |
int palindrome = 0; | |
boolean palindromeFound = false; | |
char[] arr; | |
/* Increment the natural number is a palindrome not found */ | |
while (!palindromeFound) { | |
arr = String.valueOf(naturalNumber).toCharArray(); | |
if (istPalindrom(arr) == true) { | |
palindrome = Integer.parseInt(String.valueOf(arr)); | |
palindromeFound = true; | |
} else { | |
naturalNumber++; | |
} | |
} | |
/* Out */ | |
System.out.println(palindrome); | |
} | |
/* | |
* Check number for palindrome. First loop iteration we will compare | |
* word[i1] and word[i2]. They're equal, so we increment i1 and decrement | |
* i2, then compare others. | |
*/ | |
public static boolean istPalindrom(char[] numbers) { | |
int i1 = 0; | |
int i2 = numbers.length - 1; | |
while (i2 > i1) { | |
if (numbers[i1] != numbers[i2]) { | |
return false; | |
} | |
++i1; | |
--i2; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment