package cse1030.Mar12;

public class Recursion {

	// Print a given number of * to the screen in a horizontal line
	public static void starsNR(int n) {
		for (int i = 0; i < n; i++) {
			System.out.print("*");
		}
		System.out.println("");
	}
	
	public static void stars(int n) {
		// precondition: n >= 0
		// break this into two problems:
		// 1. print "*"
		// 2. print n-1 stars in a horizontal line
		
		if (n == 0) {
			// stopping case
			System.out.println("");
		}
		else {
			// recursive case
			System.out.print("*");
			stars(n-1);
		}
		
	}
	
	// print the string backwards: "Hello World!" becomes "!dlroW olleH"
	public static void backwardsNR(String s) {
		for (int i = s.length()-1; i >= 0; i--) {
			System.out.print(s.charAt(i));
		}
		System.out.println("");
	}
	
	public static void backwards(String s) {
		// print the last character in the string
		// print backwards the rest of the characters
		// e.g. "Hello World!":
		// print "!", then print "Hello World" backwards
		if (s.length() == 0) {
			// stopping case ... nothing to do
			
			System.out.println("");
		}
		else {
			// recursive case
			
			// strings start at index 0 and go to length()-1
			System.out.print(s.charAt(s.length()-1));
			// we want the string from 0 to s.length()-2
			backwards(s.substring(0,s.length()-1));
		}
		
	}
	
	// Calculate the factorial of the given integer: n!
	public static int factorialNR(int n) {
		
		int x = 1;
		for (int i = 1; i <= n; i++) {
			x = x * i;
		}
		return x;
		
	}
	
	public static int factorial(int n) {
		// precondition: n >= 1
		
		if (n == 1)
			return 1;
		
		return n * factorial(n-1);
		
	}
	
	public static void main(String[] args) {
		//starsNR(10);
		//stars(10);
		//backwardsNR("Hello World!");
		//backwards("Hello World!");
		System.out.println(factorialNR(10));
		System.out.println(factorial(10));
	}

}
