Power - if(y == 1 return x if(y == 0 return 1 int halfpower...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
public class Power{ public static void main(String[] args){ System.out.println(power(3,8)); System.out.println(power(3,5)); System.out.println(power(3,0)); for(int i = 0; i<20; i++){ System.out.println("\n" + i + "\t" + power(3,i)); } } public static int power(int x, int y){
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: if (y == 1) return x; if (y == 0) return 1; int halfpower = power(x, y/2); if ((y % 2) == 0) { System.out.print("*"); return halfpower * halfpower; } else { System.out.print("**"); return halfpower * halfpower * x; } } }...
View Full Document

This note was uploaded on 07/12/2009 for the course CS 111 taught by Professor Hari during the Fall '08 term at Rutgers.

Ask a homework question - tutors are online