Write a program that prints all the palindrome till 1000 in java


In this core java programming tutorial we will Write a program that prints all the palindrome till 1000 in java.



Q.What is palindrome?
A. If reverse of number is same as original one, than its palindrome or palindrome is a number which reads the same backward or forward.

Example in java>
input : 121
output: it’s palindrome.


Logic used here will offer you best time and space complexity in java.

time complexity  in java-
taken for each number will be O(n/2).
so, overall complexity of program is 1000 x O(n/2), where n will change from 1 to 1000.
space complexity  in java-
because we are using only one variable i.e. j for palindrome calculation of each number. (i is not counted because it is just used for maintaining 1 to 1000 count)



Full Program/SourceCode that prints all the palindrome till 1000 in java >
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class PalindromeTillExample {
   public static void main(String[] args) {
          System.out.println("List of Palindrome between 1 to 1000");
          for(int i=10; i<=1000; i++){
                 char[] ar = String.valueOf(i).toCharArray();
                 boolean isPalindrome = true;
                 for(int j=0;j<ar.length/2;j++){
                       if(ar[j]!=ar[ar.length-1-j]){
                              isPalindrome =false;
                              break;
                       }
                 }
                 if(isPalindrome)
                       System.out.println(i);               
          }
   }
}
/*
List of Palindrome between 1 to 1000
11
22
33
44
55
66
77
88
99
101
111
121
131
141
151
161
171
181
191
202
212
222
232
242
252
262
272
282
292
303
313
323
333
343
353
363
373
383
393
404
414
424
434
444
454
464
474
484
494
505
515
525
535
545
555
565
575
585
595
606
616
626
636
646
656
666
676
686
696
707
717
727
737
747
757
767
777
787
797
808
818
828
838
848
858
868
878
888
898
909
919
929
939
949
959
969
979
989
999
*/



So in this core java programming tutorial we learned how to Write a program that prints all the palindrome till 1000 in java.


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.




RELATED LINKS>





eEdit
Must read for you :