Computer >> Máy Tính >  >> Lập trình >> Java

Chương trình Java để tìm tổng các số tự nhiên bằng cách sử dụng đệ quy

Trong bài này, chúng ta sẽ hiểu cách tìm tổng các số tự nhiên bằng cách sử dụng đệ quy. Tất cả các số dương có thể có từ 1 đến vô cùng được gọi là số tự nhiên. Một hàm đệ quy là một hàm gọi chính nó nhiều lần cho đến khi một điều kiện cụ thể được thỏa mãn.

Đệ quy là quá trình lặp lại các mục theo cách tương tự. Trong ngôn ngữ lập trình, nếu một chương trình cho phép bạn gọi một hàm bên trong cùng một hàm, thì nó được gọi là lệnh gọi đệ quy của hàm.

Nhiều ngôn ngữ lập trình thực hiện đệ quy bằng các ngăn xếp. Nói chung, bất cứ khi nào một hàm (người gọi) gọi một hàm khác (callee) hoặc chính nó là callee, thì hàm người gọi sẽ chuyển quyền kiểm soát thực thi đến callee. Quá trình chuyển này cũng có thể liên quan đến một số dữ liệu được chuyển từ người gọi đến người gọi.

Dưới đây là một minh chứng về điều tương tự -

Đầu vào

Giả sử đầu vào của chúng tôi là -

Enter the number : 25

Đầu ra

Đầu ra mong muốn sẽ là -

The sum of natural numbers up to 25 is 325

Thuật toán

Step 1 - START
Step 2 - Declare 2 integer values namely my_input and my_sum.
Step 3 - Read the required values from the user/ define the values
Step 4 - A recursive function ‘Add’ is defined which takes an integer as input and returns the sum of the input value and its previous value until the input value is reduced to 0.
Step 5 - The recursive function is called and the value ‘my_input’ is passed to it. Store the return value.
Step 6 - Display the result
Step 7 - Stop

Ví dụ 1

Ở đây, đầu vào đang được người dùng nhập dựa trên lời nhắc. Bạn có thể thử trực tiếp ví dụ này trong công cụ nền tảng mã hóa của chúng tôi Chương trình Java để tìm tổng các số tự nhiên bằng cách sử dụng đệ quy .

import java.util.Scanner;
public class NaturalNumbers {
   public static void main(String[] args) {
      int my_input, my_sum;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter the number : ");
      my_input = my_scanner.nextInt();
      System.out.println("The number is defined as " +my_input);
      my_sum = Add(my_input);
      System.out.println("The sum of natural numbers up to " + my_input + " is " + my_sum);
   }
   public static int Add(int my_input) {
      if (my_input > 0)
         return my_input + Add(my_input - 1);
      else
         return my_input;
   }
}

Đầu ra

Required packages have been imported
A reader object has been defined
Enter the number : 25
The number is defined as 25
The sum of natural numbers up to 25 is 325

Ví dụ 2

Ở đây, số nguyên đã được xác định trước đó và giá trị của nó được truy cập và hiển thị trên bảng điều khiển.

public class NaturalNumbers {
   public static void main(String[] args) {
      int my_input, my_sum;
      my_input = 25;
      System.out.println("The number is defined as " +my_input);
      my_sum = Add(my_input);
      System.out.println("The sum of natural numbers up to " + my_input + " is " + my_sum);
   }
   public static int Add(int my_input) {
   if (my_input > 0)
      return my_input + Add(my_input - 1);
   else
      return my_input;
   }
}

Đầu ra

The number is defined as 25
The sum of natural numbers up to 25 is 325