





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






import java.io.; class Recursion { public static int SumSquares (int m, int n) { if (m == n) return mm; else return n*n + SumSquares(m,n-1); } public static void main(String [] args) { int i=10; int j=20; for (int k=i; k<=j; k++) { System.out.println("Sum_of_squares from " + i +" to "+ k +" "+
SumSquares(i,k)); } } } Output: Sum_of_squares from 10 to 10 100 Sum_of_squares from 10 to 11 221 Sum_of_squares from 10 to 12 365 Sum_of_squares from 10 to 13 534 Sum_of_squares from 10 to 14 730 Sum_of_squares from 10 to 15 955 Sum_of_squares from 10 to 16 1211 Sum_of_squares from 10 to 17 1500 Sum_of_squares from 10 to 18 1824 Sum_of_squares from 10 to 19 2185 Sum_of_squares from 10 to 20 2585
public static int SumSquares (int m, int n) { if (m == n) return m*m; else { int middle = (m+n)/2; return (SumSquares (m, middle) + SumSquares(middle+1,n)); } }
public static void Pattern (int n) { if (n == 1) System.out.println(''); else { for (int i=1; i<=n ; i++) System.out.print(''); System.out.println(); Pattern(n-1); }**
public static void Pattern (int n) { if (n == 1) System.out.println(''); else { pattern(n-1); for (int i=1; i<=n ; i++) System.out.print(''); System.out.println(); } }**
import java.io.; import java.util.; Time
Source Spare Sink
public class Tower { public static void move(int n, int source, int spare, int sink) { if (n==1) System.out.println("Move "+source+" -> "+ sink); else { move(n-1, source, sink, spare); move(1, source, spare, sink); move(n-1, spare, source, sink); } } public static void main (String [] args) { int n=4; int source=1; int spare=2; int sink=3; move(n, source, spare, sink); } }