Latest Updates ☛
  • Graphs: Introduction of graphs and types of graphs
  • SPOJ : Geeky solves geeky solution
  • Solution Code : Pass the time problem on SPOJ.
  • Find the minimum shortest path solution in c language ! !

Tuesday, August 13, 2013

Java Solution for Next Pallindrome number finder !!

Next Pallindrome Number Finder


This question i received while solving a problem on SPOJ so i made a solution to this question and wanted to give this idea to other fellows the question was like that----

"A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 10000000 digits, write the value of the smallest palindrome larger than
                                                      K to output.Number  are always displayed without leading zeros."


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
59
60
61
62
63
64
65
66
//Java program to print next pallindrome number of given number K where 0<=k<=100000000 

/**
* @author Code Author
*/
import java.util.*;
class Pallindrome {

//method to find if number ids pallindrome or not and return either true or false
static boolean next_pallin(int k)
{
boolean flag = true;

//converting the number into string type
String str =String.valueOf(k);

//getting the length of the string
int c = str.length();

//dividing the string length
int m = c/2;
int i=0;

while(m>0)
{
//checking char at first and last from middle
if(str.charAt(i)!=str.charAt(c-i-1))
flag=false;

i++;
m--;
}

//returning the status os string i.e number k
return flag;
}


public static void main(String[] args) {
Scanner console =new Scanner(System.in);

//getting number of test cases
int t = console.nextInt();
int k;

while(t>0)
{
k =console.nextInt(); //input for the k

/* Here we apply do-while loop but not while loop bcoz.
* we have to check the next number is pallindrome or not
* but not the given number inorder to do so we always
* start with k+1 value so intial increment is must else if apply
* while loop it will print the same number for k and
* if we apply for k+1 it will always bypass a number */

do k++;
while(!next_pallin(k));

//printing next pallindrome number
System.out.println(k);
t--;
}
}

}


Here the first line contains the test conditions for which wwe have to find the palindromes and the next line should be as given accordingly

The OUTPUT of the code is here -----


Enter number of test cases : 5
Enter your number : 0
The next pallindrome number is : 1

Enter your number : 123
The next pallindrome number is : 131

Enter your number : 3123
The next pallindrome number is : 3223

Enter your number : 947485
The next pallindrome number is : 947749

Enter your number : 16928683
The next pallindrome number is :16933961

No comments :

Post a Comment

Privacy Policy | Disclaimer | Terms of Service

Copyright © 2013, Code Author. Powered by Blogger.