import java.util.*; public class RecursionExamples { public static void main(String[] args) { printBinary(45); System.out.println(); System.out.println(isPalindrome("madam")); System.out.println(isPalindrome("Java")); } // Returns true if the given string reads the same // forwards as backwards. // Trivially true for empty or 1-letter strings. public static boolean isPalindrome(String s) { if (s.length() < 2) { return true; // base case } else { return s.charAt(0) == s.charAt(s.length() - 1) && isPalindrome(s.substring(1, s.length() - 1)); } } // Prints the given integer's binary representation. public static void printBinary(int n) { if (n < 0) { // recursive case for negative numbers System.out.print("-"); printBinary(-n); } else if (n < 2) { // base case; same as base 10 System.out.print(n); } else { // recursive case; break number apart printBinary(n / 2); printBinary(n % 2); } } }