Java 程序:使用递归求N个数总和

原文:https://www.studytonight.com/java-programs/java-program-to-find-sum-of-n-numbers-using-recursion

在本教程中,我们将看到如何使用递归找到 N 个数字的和。递归函数是调用自身的函数。但是在继续之前,如果你不熟悉 java 中循环的概念,那么一定要查看关于 Java 中循环的文章。

输入:输入数字:6 7 4 5 3

输出:所有输入的数字之和为:25

程序 1:用递归求 N 个数的和

在这个程序中,我们将看到如何使用递归计算 N 个数的和。这里,我们将考虑函数中的长度变量作为变化参数。

算法:

  1. 开始

  2. 创建扫描仪类的实例。

  3. 声明一个变量来存储数组的长度。

  4. 要求用户初始化数组大小。

  5. 声明一个变量来存储数组元素。

  6. 要求用户初始化数组元素。

  7. 调用递归函数计算总和。

  8. 将长度变量视为函数中的变化参数。

  9. 递归调用函数来计算总和。

  10. 显示计算出的总和。

  11. 停下来。

让我们看看下面的例子,以更好地理解上述算法。

//Java Program to Find Sum of N Numbers Using Recursion
import java.util.*;

public class Main 
{
    // recursive function
     public static int calculate_sum(int arr[], int length)
    {
        // base condition - when reached -1 index return 0
        if (length == -1) 
        {
            return 0;
        }
        // Call the function recursively to calculate the sum
        return arr[length] + calculate_sum(arr,length - 1);

    }
    //Driver Code
    public static void main(String[] args)
    {
        //Create an instance of the Scanner Class
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the array size: ");
        int n=sc.nextInt();
        int total_sum = 0;
        //Array Creation and Initialization
        int arr[] = new int[n];
        System.out.println("Enter the array elements: ");
        for(int i=0;i<n;i++)
        {
            arr[i]=sc.nextInt();
        }
        // call function to calculate sum
        total_sum = calculate_sum(arr, n-1);
        System.out.println("The total of N numbers is : "+ total_sum);
    }
}

输入数组大小:5 输入数组元素:4 7 6 5 3 N 个数的总和为:25

程序 2:用递归求 N 个数的和

在这个程序中,我们将看到如何使用递归计算 N 个数的和。在这里,我们将从向前的方向开始递归,并在结束/最后的位置到达和命中基础条件。

算法:

  1. 开始

  2. 创建扫描仪类的实例。

  3. 声明一个变量来存储数组的长度。

  4. 要求用户初始化数组大小。

  5. 声明一个变量来存储数组元素。

  6. 要求用户初始化数组元素。

  7. 调用递归函数计算总和。

  8. 从正向开始递归。

  9. 递归调用函数来计算总和。

  10. 显示计算出的总和。

  11. 停下来。

让我们看看下面的例子,以更好地理解上述算法。

//Java Program to Find Sum of N Numbers Using Recursion
import java.util.*;

public class Main 
{
    // recursive function
    public static int calculate_sum(int arr[], int i, int length)
    {
        // base condition - when reached end of the array
        // return 0
        if (i == length) {
            return 0;
        }
        // recursive condition - current element + sum of
        // (n-1) elements
        return arr[i]
         + calculate_sum(arr, i + 1,length);

    }
    //Driver Code
    public static void main(String[] args)
    {
        //Create an instance of the Scanner Class
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the array size: ");
        int n=sc.nextInt();
        int total_sum = 0;
        //Array Creation and Initialization
        int arr[] = new int[n];
        System.out.println("Enter the array elements: ");
        for(int i=0;i<n;i++)
        {
            arr[i]=sc.nextInt();
        }
        // call function to calculate sum
        total_sum = calculate_sum(arr,0,n);
        System.out.println("The total of N numbers is : "+ total_sum);
    }
}

输入数组大小:5 输入数组元素:2 6 4 7 8 N 个数的总和为:27