Programming and Problem Solving with Java

(やまだぃちぅ) #1
741

Chapter 13 Programming Warm-Up Exercises


1.public static intf(intn)
{
if(n == 0 || n == 1 )
return 1 ;
else
returnf(n - 1 ) + f(n - 2 );
}
4.public static intfactorial(int n, intlevel)
{
inttempFactorial;
intloopCount;
for(loopCount = 1 ; loopCount <= level; loopCount++)
System.out.print(" ");
System.out.println(n);
if(n == 0 )
tempFactorial = 1 ;
else
tempFactorial = n * factorial(n - 1 , level + 1 );
for(loopCount = 1 ; loopCount <= level; loopCount++)
System.out.print(" ");
System.out.println(tempFactorial);
returntempFactorial;
}
6.public static voidprintSqRoots(intn)
{
if(n > 0 )
{
outFile.println(n + " "+ Math.sqrt((double)n));
printSqRoots(n - 1 );
}
}
7.public static voidprint(
int[] data, // Array to be printed
intfirst, // Index of first element
intlast ) // Index of last element
// Print an array
Free download pdf