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

Chương trình Java để tìm sản phẩm của hai số bằng cách sử dụng đệ quy

Trong bài này, chúng ta sẽ hiểu cách tìm tích của hai số bằng cách sử dụng đệ quy. 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 two number : 12 and 9

Đầu ra

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

The product of 12 and 9 is 108

Thuật toán

Step 1 - START
Step 2 – Declare two integer values namely my_input and my_result
Step 3 - Read the required values from the user/ define the values
Step 4 - A recursive function ‘getproduct’ is defined which takes two integers as input. The function computes the reminder by re-iterating over the function multiple times, until the base condition is reached.
Step 5 - The recursive function ‘getproduct’ is called and its result is stored
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 sản phẩm của hai số bằng cách sử dụng đệ quy .

import java.util.Scanner;
public class ProductRecursion{
   public static void main (String[] args){
      int my_input_1, my_input_2;
      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_1 = my_scanner.nextInt();
      System.out.print("Enter the number : ");
      my_input_2 = my_scanner.nextInt();
      System.out.println("The product of "+my_input_1 +" and " +my_input_2 +" is " +getproduct(my_input_1, my_input_2));
   }
   static int getproduct(int my_input_1, int my_input_2){
      if (my_input_1 < my_input_2)
         return getproduct(my_input_2, my_input_1);
      else if (my_input_2 != 0)
          return (my_input_1 + getproduct(my_input_1, my_input_2 - 1));
      else
         return 0;
   }
}

Đầu ra

Required packages have been imported
A reader object has been defined
Enter the number : 12
Enter the number : 9
The product of 12 and 9 is 108

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 ProductRecursion{
   public static void main (String[] args){
      int my_input_1, my_input_2;
      my_input_1 = 12;
      my_input_2 = 9;
      System.out.println("The two numbers are defined as " +my_input_1 +" and " +my_input_2);
      System.out.println("The product of "+my_input_1 +" and " +my_input_2 +" is " +getproduct(my_input_1, my_input_2));
   }
   static int getproduct(int my_input_1, int my_input_2){
      if (my_input_1 < my_input_2)
         return getproduct(my_input_2, my_input_1);
      else if (my_input_2 != 0)
         return (my_input_1 + getproduct(my_input_1, my_input_2 - 1));
      else
         return 0;
   }
}

Đầu ra

The two numbers are defined as 12 and 9
The product of 12 and 9 is 108