Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Lab 3 – Recursion Exercises 1- Write a recursive method that finds the minimum value in an array of integer values without using any loops. package recursivemin; public class RecursiveMin { public static void main(String[] args) { int ar[] = {4,6,2,8,4,22,4,3,6,13}; int m=ar[0]; System.out.println("The elements of an array is: "); for (int i = 0; i<ar.length;i++){ System.out.print(ar[i]+" "); } System.out.println(); min(ar,ar.length,m); } public static void min(int a[],int l,int m){ if (l==1){ System.out.println("The min is: "+m); } else{ if (m>a[l-1]) m=a[l-1]; min(a,l-1,m); } } } 2- Write a recursive function/method that displays an int value reversely. For example, reverse(12345) displays 54321. Prompt the user to enter the value. package recurreverse; import java.util.Scanner; public class RecurReverse { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter a number bigger than 100: "); int x = input.nextInt(); System.out.println("The reversed number is: " ); reverse(x); } public static void reverse(int a){ if (a==0) System.out.println(); else { int s = a%10; System.out.print(s); reverse(a/10); } } } 3- Write a recursive function/method that sums the digits of an integer. For example, sum(123) will give the result 6. Prompt the user to enter the integer number. package javaapplication8; import java.util.Scanner; public class JavaApplication8 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int sum = 0; System.out.println("Enter a number bigger than 100: "); int x = input.nextInt(); System.out.println("The sum of numbers is: " + sumBt(x,sum) ); } public static int sumBt(int a,int s){ if (a==0) return s; else { s = s+(a%10); return sumBt(a/10,s); } } }