Results 1 to 5 of 5
 10202014, 05:57 PM #1Member
 Join Date
 Apr 2014
 Posts
 6
 Rep Power
 0
Creating Palindromes Using Recursion
Hello, I need help.
I just started studying recursion and I wanted to know how to create a palindrome number going up from 1 to n then back to 1 like this: "12345...n...54321".
I've done one going downwards and then upwards like this: "n...4321234...n".
Here's my code:
Java Code:import java.util.*; public class PalindromeTest { public static void downPalindrome(int n) { if(n>0) { System.out.print(n); downPalindrome(n1); if(n!=1) System.out.print(n); } } public static void main(String args[]) { Scanner in = new Scanner (System.in); int x=in.nextInt(); downPalindrome(x); } }
Can anyone help?
Thanks a lot!
 10202014, 06:32 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,423
 Blog Entries
 7
 Rep Power
 27
Re: Creating Palindromes Using Recursion
Let's do the first type of palindromes first (the other one is similar): assume a current number i and a maximum number n; if i == n, the inner part of the palindrome is just n; otherwise, the inner part of the palindome is P(i+1, n) surrounded by i, both on the left and the right part, i.e. P(i, n) == if (i == n) then n else i P(i+1, n) i. An example: n == 3, so P(1, 3) == 1 P(2, 3) 1 == 1 2 P(3, 3) 2 1 == 1 2 3 2 1. It doesn't take much to turn this to working Java code.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
 10202014, 06:46 PM #3Member
 Join Date
 Apr 2014
 Posts
 6
 Rep Power
 0
Re: Creating Palindromes Using Recursion
Thanks! Although I was trying to create it using only 1 perimeter.
 10202014, 07:23 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,423
 Blog Entries
 7
 Rep Power
 27
Re: Creating Palindromes Using Recursion
But then you have to create two methods: one for the left part and one for the right part of the palindrome and both can be recursive. You can hide one parameter from the world by doing this:
P(n) == P(1,n), where the second method does all the work and the first method is just a convenience method for the user.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
 10202014, 08:05 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,423
 Blog Entries
 7
 Rep Power
 27
Re: Creating Palindromes Using Recursion
p.s. you can do the second type of palindrome with a one parameter method. In general: if the sentinel value (either 0 or n) can be considered some constant value, both palindrome types can be done with a one parameter method.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
Similar Threads

Creating recursion method to use Newton's method for square roots
By bdl1127 in forum New To JavaReplies: 2Last Post: 03232012, 04:53 AM 
Problem with Palindromes
By thaotic in forum New To JavaReplies: 5Last Post: 03142012, 12:01 PM 
Counting number of palindromes in a file
By vuzuggu in forum New To JavaReplies: 3Last Post: 10202011, 05:52 PM 
Project Euler #4  Palindromes
By Awk34 in forum New To JavaReplies: 12Last Post: 12192010, 10:28 AM 
Find All Palindromes
By spleenlol in forum New To JavaReplies: 6Last Post: 02042010, 02:33 AM
Bookmarks