কম্পিউটার

জাভা প্রোগ্রাম রিকারশন ব্যবহার করে সংখ্যার যোগফল খুঁজে বের করতে


এই প্রবন্ধে, আমরা বুঝব কিভাবে পুনরাবৃত্তি ব্যবহার করে N সংখ্যার যোগফল বের করা যায়। একটি পুনরাবৃত্ত ফাংশন এমন একটি ফাংশন যা একটি নির্দিষ্ট শর্ত সন্তুষ্ট না হওয়া পর্যন্ত নিজেকে একাধিকবার কল করে৷

Recursion হল একটি স্ব-অনুরূপ উপায়ে আইটেম পুনরাবৃত্তি করার প্রক্রিয়া। প্রোগ্রামিং ভাষায়, যদি একটি প্রোগ্রাম আপনাকে একই ফাংশনের ভিতরে একটি ফাংশন কল করার অনুমতি দেয়, তাহলে তাকে ফাংশনের একটি পুনরাবৃত্ত কল বলা হয়।

অনেক প্রোগ্রামিং ল্যাঙ্গুয়েজ স্ট্যাকের মাধ্যমে পুনরাবৃত্তি বাস্তবায়ন করে। সাধারণত, যখনই একটি ফাংশন (কলার) অন্য একটি ফাংশন (ক্যালি) বা নিজেকে কলী হিসাবে কল করে, কলারের ফাংশনটি এক্সিকিউশন কন্ট্রোল ক্যালিতে স্থানান্তর করে। এই স্থানান্তর প্রক্রিয়াটিতে কলকারী থেকে কলকারীর কাছে পাঠানোর জন্য কিছু ডেটাও জড়িত থাকতে পারে।

নীচে একই -

এর একটি প্রদর্শন রয়েছে৷

ইনপুট

ধরুন আমাদের ইনপুট হল −

Enter the value of N : 6
Enter the elements of array :
15 30 45 80 100 140

আউটপুট

কাঙ্খিত আউটপুট হবে −

The total of N numbers is : 410

অ্যালগরিদম

Step 1 - START
Step 2 - Declare two integer values namely N , my_sum and i and an integer array ‘my_array’
Step 3 - Read the required values from the user/ define the values
Step 4 - A recursive function ‘RecursiveSum 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 ‘RecursiveSum is called and its result is stored
Step 6 - Display the result
Step 7 - Stop

উদাহরণ 1

এখানে, একটি প্রম্পটের উপর ভিত্তি করে ব্যবহারকারী দ্বারা ইনপুট প্রবেশ করানো হচ্ছে। আপনি আমাদের কোডিং গ্রাউন্ড টুলে এই উদাহরণ লাইভ চেষ্টা করতে পারেন জাভা প্রোগ্রাম রিকারশন ব্যবহার করে সংখ্যার যোগফল খুঁজে বের করতে

import java.util.Scanner;
public class ArraySum {
   public static int RecursiveSum(int my_array[], int i,int N){
      if (i == N)
         return 0;
      return my_array[i] + RecursiveSum(my_array, i + 1,N);
   }
   public static void main(String[] args){
     int N, my_sum, i;
     N = 6;
     my_sum = 0;
     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 value of N : ");
     N = my_scanner.nextInt();
     int my_array[] = new int[N];
     System.out.println("Enter the elements of the array :" );
     for ( i = 0 ; i < N ; i++ ){
        my_array[i] = my_scanner.nextInt();
     }
     my_sum = RecursiveSum(my_array, 0, N);
     System.out.println("\n The total of N numbers is : " + my_sum);
   }
}

আউটপুট

Required packages have been imported
A reader object has been defined
Enter the value of N : 6
Enter the elements of the array :
15
30
45
80
100
140
The total of N numbers is : 410

উদাহরণ 2

এখানে, পূর্ণসংখ্যা পূর্বে সংজ্ঞায়িত করা হয়েছে, এবং এর মান অ্যাক্সেস করা হয়েছে এবং কনসোলে প্রদর্শিত হয়েছে।

public class Main {
   public static void main(String[] args) {
      int[] my_array = {15, 20, 25, 30, 35, 40};
      int my_input , i, array_size;
      array_size = 5;
      my_input = 25;
      boolean my_check = false;
      System.out.println("The number is defined as " +my_input);
      System.out.println("The elements in the integer array is defined as :" );
      for ( i = 0 ; i < array_size ; i++ ){
         System.out.print(my_array[i] +" ");
      }
      for ( i = 0 ; i < array_size ; i++ ) {
         if (my_array[i] == my_input) {
            my_check = true;
            break;
         }
      }
     if(my_check)
        System.out.println("\nThe array contains the given value");
     else
        System.out.println("\nThe array doesnot contain the given value");
   }
}

আউটপুট

The number is defined as 25
The elements in the integer array is defined as :
15 20 25 30 35
The array contains the given value

  1. দুটি সংখ্যার যোগফল এবং পার্থক্য বের করার জন্য সি প্রোগ্রাম

  2. পুনরাবৃত্ত ফাংশন ব্যবহার করে সংখ্যার GCD খুঁজে বের করতে C প্রোগ্রাম

  3. জাভা প্রোগ্রাম প্রথম n প্রাকৃতিক সংখ্যার কিউব যোগফল

  4. পাইথনে পুনরাবৃত্তি ব্যবহার করে প্রাকৃতিক সংখ্যার যোগফল কীভাবে খুঁজে পাওয়া যায়?