Recursion | Recursion on Arrays | Max Value | Sum of arrays | Lecture 32 | Java & DSA Course

 J    A    V    A             C    O    D     E

       1. Write a program to print all the values in an Array Recursively       

     </>   RecArrayPrinting.java     

  1. // Printing ARRAY elements :

  2. package Recursion;
  3. import java.util.Scanner;

  4. public class RecArrayPrinting {
  5. public static void print(int[]arr,int n){
  6. if(n==0) return ;
  7. print(arr,n-1);
  8. System.out.print(arr[n-1]+" ");

  9. }
  10. public static void main(String[] args) {
  11. Scanner sc=new Scanner(System.in);
  12. System.out.println("Enter no. of elements: ");
  13. int n=sc.nextInt();
  14. int []arr=new int[n];
  15. for(int i=0;i<n;i++)
  16. arr[i]=sc.nextInt();
  17. print(arr,n);

  18. sc.close();
  19. }
  20. }

      2. Write a program to find the Maximun value in an Array by Recursively       

      </>   RecArrayMaxFnd.java      

  1. // FINDING MAXIMUM OF ARRAY :

  2. package Recursion;
  3. import java.util.Scanner;

  4. public class RecArrayMaxFnd {
  5. public static void fndMax(int []arr,int n,int max){
  6. if(max<arr[n]) max=arr[n];
  7. if(n==0){
  8. System.out.println("Max. value of this array is: "+max);
  9. return;
  10. }
  11. fndMax(arr,n-1,max);

  12. }
  13. public static void main(String[] args) {
  14. Scanner sc=new Scanner(System.in);
  15. System.out.println("Enter no. of elements: ");
  16. int n=sc.nextInt();
  17. int []arr=new int[n];
  18. for(int i=0;i<n;i++)
  19. arr[i]=sc.nextInt();
  20. int max=arr[n-1];
  21. fndMax(arr,n-2,max);

  22. sc.close();

  23. }
  24. }

      3. Write a program to find the Sum of all elements in an Array by Recursively       

      </>   RecArraySum.java     

  1. // FINDING SUM OF ALL ELEMENT IN AN ARRAY :

  2. package Recursion;
  3. import java.util.Scanner;

  4. public class RecArraySum {
  5. public static int addAll(int []arr,int n,int sum){
  6. sum=sum+arr[n];
  7. if(n==0) return sum;
  8. return addAll(arr,n-1,sum);

  9. }
  10. public static void main(String[] args) {
  11. Scanner sc=new Scanner(System.in);
  12. System.out.println("Enter no. of elements: ");
  13. int n=sc.nextInt();
  14. int []arr=new int[n];
  15. for(int i=0;i<n;i++)
  16. arr[i]=sc.nextInt();
  17. int ans=addAll(arr,n-2,arr[n-1]);
  18. System.out.println(ans);
  19. sc.close();
  20. }

  21. }

😎 SEE YOU AGAIN !!

Comments

Popular posts from this blog

Recursion | GCD | Euclids Algorithm | Lecture 31 | Java & DSA Course

Recursion | Linear Search | Find all indices | Lecture 33 | Java and DSA Course

S O M E | B A S I C | M E T H O D S | O F | S t r i n g | I N | J A V A :