import java.io.*; // this class demonstrate some of the popular recursive ideas public class recursive { public static void main(String[] argv) { System.out.println(" ---- dish wash : ----"); dishWash(5); // there are five dishes to wash System.out.println(" ---- factorial : ----"); System.out.println(" factorial 6 should be 720"); System.out.println(" factorial 6 is equal to : " + factorial(6)); System.out.println(" ---- summation : ----"); System.out.println(" summation 6 should be 21"); System.out.println(" summation 6 is equal to : " + sum(6)); System.out.println(" ---- exponentiation : ----"); System.out.println(" power (3,4) should be 81"); System.out.println(" power(3,4) is equal to : " + power(3,4)); System.out.println(" test finished ..."); } // main static void dishWash(int count) { if (count == 0) { System.out.println(" dish wash completes."); } else { // wash one dish System.out.println(" There are " + count + " more dishes to wash ..."); System.out.println(" Mmm, wash one dish "); /*** System.out.print(" this is the " + count); switch (count) { case 1 : System.out.print(" -st "); break; case 2 : System.out.print(" -nd "); break; case 3 : System.out.print(" -rd "); break; default : System.out.print(" -th "); } System.out.println(" one"); ***/ dishWash(count - 1); // recursive call } // if-then-else } // dishWash static int factorial(int x) { if (x == 0) return 1; else return x * factorial(x - 1); } // factorial static int sum(int x) { if (x == 0) return 0; else return x + sum(x - 1); } // sum static int power(int x, int p) { if (p == 0) return 1; else return x * power(x, p - 1); } // power }