Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a recursive function that computes the sum of all numbers from 1 to n,

ID: 3842353 • Letter: 1

Question

1. Write a recursive function that computes the sum of all numbers from 1 to n, where n is given as parameter.

Here is the method header: public static int sum (int n){...}


2. Write a recursive function that finds and returns the minimum value in an array, where the array and its size are given as parameters.

Here is the method header: public static int minValue (int [] data, int size){...}

3. Write a recursive function that reverses the elements in an array, where the array, indices of the first and the last value in the array are given as parameters.

Here is the method header: public static void reverse (int [] data, int start, int end){...}

Explanation / Answer

1) Function

/* This method will calculate the sum of numbers
   * from 1 to user entered number using recursive method
   */
   private static int sum(int n) {
       if (n == 1) {
           return 1;
       } else {
           return n + sum(n - 1);
       }
   }

___________________

Complete Program:

import java.util.Scanner;

public class RecursiveSum {

   public static void main(String[] args) {
       //Declaring variables
       int n;

       // Scanner object is used to get the inputs entered by the user
       Scanner sc = new Scanner(System.in);

       //Getting the number entered by the user
       System.out.print("Enter the number :");
       n = sc.nextInt();

       //calling the method by passing the input as argument
       int res = sum(n);
      
       //Displaying the result
       System.out.println("The sum of numbers from 1 to " + n + " is :" + res);

   }

   /* This method will calculate the sum of numbers
   * from 1 to user entered number using recursive method
   */
   private static int sum(int n) {
       if (n == 1) {
           return 1;
       } else {
           return n + sum(n - 1);
       }
   }

}

____________________

Output:

Enter the number :10
The sum of numbers from 1 to 10 is :55

____________________

2) Function

//This method finds the minimum value of an array using recursion
   private static int minValue(int[] data, int size) {
       if (size==1) {
       return data[0];
       }
return Math.min(minValue(data,size-1),data[size-1]);
   }

__________________

Complete Program:

import java.util.Scanner;

public class FindMin {

   public static void main(String[] args) {
       //Declaring variables
       int size;
  
       //Scanner object is used to get the inputs entered by the user
               Scanner sc=new Scanner(System.in);
  
               //getting the size of an array entered by the user
               System.out.print("How many numbers you want to enter :");
               size=sc.nextInt();
              
               //Creating an Integer array based on the size entered by the user
               int data[]=new int[size];
              
               /* Getting the numbers entered by the user
               * and populate those values into an array
               */
               for(int i=0;i<size;i++)
               {
                   System.out.print("Enter the number#"+(i+1)+" :");
                   data[i]=sc.nextInt();
               }
      
               //Calling the method by passing the array and its size as arguments
       int minimum=minValue (data,size);
      
       //Displaying the minimum value
       System.out.println("The Minimum value is :"+minimum);
  
   }

   //This method finds the minimum value of an array using recursion
   private static int minValue(int[] data, int size) {
       if (size==1) {
       return data[0];
       }
return Math.min(minValue(data,size-1),data[size-1]);
   }

}

________________________

Output:

How many numbers you want to enter :5
Enter the number#1 :45
Enter the number#2 :52
Enter the number#3 :13
Enter the number#4 :78
Enter the number#5 :89
The Minimum value is :13

__________________

3) Function:

   //This method will reverse the elements of an array using recursion
   private static int reverse(int[] data, int start, int end) {
       if(start<end)
       {
           int tmp=data[start];
           data[start]=data[end];
           data[end]=tmp;
           reverse(data,++start,--end);
       }
       return 0;
   }

____________________

Complete Program:

import java.util.Scanner;

public class ReverseArrayElements {

   public static void main(String[] args) {
       //Declaring variables
               int size;
          
               //Scanner object is used to get the inputs entered by the user
                       Scanner sc=new Scanner(System.in);
          
                       //getting the size of an array entered by the user
                       System.out.print("How many numbers you want to enter :");
                       size=sc.nextInt();
                      
                       //Creating an Integer array based on the size entered by the user
                       int data[]=new int[size];
                      
                       /* Getting the numbers entered by the user
                       * and populate those values into an array
                       */
                       for(int i=0;i<size;i++)
                       {
                           System.out.print("Enter the number#"+(i+1)+" :");
                           data[i]=sc.nextInt();
                       }
              
                       //Displaying the elements in the array before reverse
                       System.out.println("_____ Before Reverse _____");
                       for(int i=0;i<size;i++)
                       {
                           System.out.print(data[i]+" ");
                       }
                       //Calling the method by passing the array and first and last index as arguments
               int minimum=reverse (data,0,size-1);
                       System.out.println(" _____ After Reverse _____");              
                       for(int i=0;i<size;i++)
                       {
                           System.out.print(data[i]+" ");
                       }      
                      
   }

   //This method will reverse the elements of an array using recursion
   private static int reverse(int[] data, int start, int end) {
       if(start<end)
       {
           int tmp=data[start];
           data[start]=data[end];
           data[end]=tmp;
           reverse(data,++start,--end);
       }
       return 0;
   }

}

____________________

Output:

How many numbers you want to enter :5
Enter the number#1 :12
Enter the number#2 :23
Enter the number#3 :34
Enter the number#4 :45
Enter the number#5 :56
_____ Before Reverse _____
12 23 34 45 56
_____ After Reverse _____
56 45 34 23 12

________________Could You rate me well .Plz. Thank You