Recursive methods java In Hindi

Recursive methods java In Hindi

जावा प्रोग्रामिंग लैंग्वेज में रिकर्सिव मेथड एक ऐसा प्रोग्रामिंग कन्सेप्ट या मेथड है, जो जावा या अन्य प्रोग्रामिंग लैंग्वेज में स्पेसिफिक प्रॉब्लम को डील करने के लिए अपने आप को आटोमेटिक कॉल करता है। जावा लैंग्वेज में रिकर्सन कांसेप्ट एक पावरफुल फीचर्स है, रिकर्शन कांसेप्ट का यूज़ कम्प्लेक्स न्यूमेरिकल, स्टैटिकल, या मैथमेटिक्स प्रॉब्लम को इजी करने, या लार्ज प्रॉब्लम को स्माल मॉडयूल सब-प्रॉब्लम में ब्रेक कर सॉल्व करने में किया जाता है। प्रोग्राम में रिकर्सिव टास्क तब तक चलता रहता है, जब तक यूजर डिफाइन बेस रिकर्सिव केस या टर्मिनेशन कंडीशन एक्सेक्यूट नहीं हो जाती है. जो अंत में रिकर्सिव कॉल प्रोसेस को टर्मिनेट करता है, और डिजायर रिकर्सिव मेथड के आउटपुट को डिस्प्ले करता है।

Recursive methods In Hindi

How recursion works in Java.

  • जावा प्रोग्रामिंग में एक रिकर्सिव मेथड में मुख्य रूप से दो प्रमुख एलिमेंट होते हैं.
  • Base case – यह रिकर्शन में एक यूजर डिफाइन कंडीशन है, जो रिकर्सन प्रोसेस को टर्मिनेट करती है। रिकर्शन में बेस केस के बिना, रिकर्सन प्रोसेस ऑलवेज रन होता रहेगा, जिससे प्रोग्राम में स्टैक ओवरफ्लो एरर जनरेट होता है।
  • Recursive Case – यह रिकर्शन में मेथड का इम्पोर्टेन्ट एलिमेंट है, जहाँ यूजर डिफाइन फंक्शन मेथड अपने आप को मॉडिफाइड वेरिएबल पैरामीटर आर्गुमेंट के साथ आटोमेटिक कॉल करता है, और अंत में धीरे-धीरे बेस केस की ओर मूव होता रहता है।

Structure of a recursive method in Java.

  • Base case – यह यूजर क्रिएटेड रिकर्शन प्रोसेस में वह कंडीशन है, जो रिकर्सन प्रोसेस को टर्मिनेट करती है।
  • Recursive case – यह रिकर्शन में वह एलिमेंट या पोरशन है, जहाँ रिकर्शन प्रोसेस में रिकर्सिव मेथड अपने आप को कॉल करता है, सामान्य रूप से यह ओरिजिनल लार्ज काम्प्लेक्स प्रॉब्लम को स्माल मॉडयूल या सिंपल फॉर्मेट में डिस्प्ले करता है।

<return type> <method name>(<parameters>) {

    if (<base case condition>) {

        // here it Return a value and apply user action to stop recursion process

    } else {

        // Recursive case: here it call the recursive method again with new parameters or variable

    }

}

Factorial example using recursion in Java.

फ़ैक्टोरियल एक स्टार्ट नंबर से एन्ड नंबर को अपने आप से स्टार्ट से एन्ड तक मल्टीप्लय करता है, जिसमे नंबर अपने नेक्स्ट नंबर को मल्टीप्लय कर सभी नंबर के मल्टीप्लिकशन को एक फैक्ट वैल्यू के रूप में डिस्प्ले करता है, एक फ़ैक्टोरियल नंबर n, जिसे n! के रूप में डिस्प्ले किया जाता है. फैक्टोरियल n! से कम या उसके बराबर सभी स्टार्ट से एन्ड इंटीजर न्यूमेरिक वैल्यू का मल्टिप्लिकेशन या प्रोडक्ट रिजल्ट होता है. जैसे, n!=n×(n−1)×(n−2)×⋯×1n!=n×(n−1)×(n−2)×⋯×1

Factorial example.

7! = 7×6×5×4×3×2×1 = 5,040

Example of the recursive factorial function method in Java.

public class Main

{

// here Recursive method used to calculate the factorial of a given integer number

    public static int fact(int n) {

        if (n == 0) {

            return 1;  // Base case: here we define factorial of 0 is 1

        } else {

            return n * fact(n – 1);  // here we used Recursive call method to call recursive function

        }

    }

    public static void main(String[] args) {

        int fact_number = fact(7);

        System.out.println(“\n The Factorial number of 7 is – ” + fact_number);  // Output – 5040

    }

}

Explaining the recursive factorial function.

  • यहाँ फ़ैक्टोरियल रिकर्सिव फंक्शन में एक fact रिकर्सिव फंक्शन डिफाइन किया गया है.
  • Base Case – यहाँ फैक्ट रिकर्सिव फंक्शन में यदि n 0 वैल्यू है, तो यह 1 वैल्यू रिटर्न करता है, (क्योंकि 0!=10!=1) डिफाइन है.
  • Recursive Case – यह मेथड अपने आप को n−1n−1 से तब तक कॉल करता है, जब तक n 0 वैल्यू न हो जाए।

Example of the Fibonacci sequence using the recursion method in Java.

जावा प्रोग्रामिंग में फिबोनाची नंबर सीक्वेंस में दिए गए इन्टिजर वैल्यू या नंबरों का एक कॉन्टिनियस सीक्वेंस है, जहाँ हर नंबर पिछले दो नंबरों का आपस में कन्टीन्यूस ऐड वैल्यू होता है, जो सामान्य रूप से 0 और 1 से स्टार्ट होता है। जावा में फिबोनाची नंबर सीक्वेंस कुछ इस तरह दिखता है.

F(0)=0F(0)=0

F(1)=1F(1)=1

F(n)=F(n−1)+F(n−2)F(n)=F(n−1)+F(n−2) for n≥2n≥2

Example of the recursive Fibonacci method in a Java program.

public class Main

{

// here Recursive method used to calculate the nth Fibonacci number of sequence in continuous order

    public static int fibonacci_num(int n) {

        if (n == 0) {

            return 0;  // here Base case define – F(0) = 0

        } else if (n == 1) {

            return 1;  // here Base case define – F(1) = 1

        } else {

            return fibonacci_num(n – 1) + fibonacci_num(n – 2);  // here we define fibonacci_num Recursive case

        }

    }

    public static void main(String[] args) {

        int output = fibonacci_num(8);

        System.out.println(“\n The Fibonacci_number sequence of 8 is – ” + output);  // Output is – 21

    }

}

Fibonacci sequence explanation.

  • यहाँ जावा में रिकर्सिव फिबोनाची नंबर सीक्वेंस में fibonacci_num फंक्शन मेथड डिफाइन किया गया है.
  • Base Case – यहाँ फिबोनाची फंक्शन में F(0)=0F(0)=0 और F(1)=1F(1)=1 बेस केस डिफाइन है.
  • Recursive Case – यहाँ रिकर्सिव केस में n≥2n≥2 के आउटपुट के लिए, F(n)=F(n−1)+F(n−2)F(n)=F(n−1)+F(n−2) फिबोनाची वैल्यू है।

Example of adding array elements using recursion methods in Java.

जावा यूजर जावा प्रोग्राम में यूजर डिक्लेअर ऐरे एलिमेंट्स को ऐड करने के लिए रिकर्सन फंक्शन मेथड को यूज़ कर सकते हैं।

Recursive Sum of Array Method Example in Java.

public class Main

{

// here we use Recursive function method to count the sum addition of all array elements define in given array

    public static int addArray(int[] array, int index) {

        if (index == array.length) {

            return 0;  // Base case define – here if index is at the end of the array, it return value 0

        } else {

            return array[index] + addArray(array, index + 1);  // here we use the Recursive case

        }

    }

    public static void main(String[] args) {

        int[] array = {4, 7, 9, 11, 14, 17, 19};

        int output = addArray(array, 0);

        System.out.println(“\n The Addition of all array elements – ” + output);  // Output is – 81

    }

}

Explaining the Recursive Sum of Array Method.

  • यहाँ इस प्रोग्राम में addArray ऐरे नाम से एक रिकर्सिव फंक्शन डिफाइन किया गया है.
  • Base Case – यहाँ जब इंडेक्स ऐरे की लेंथ तक पहुँच जाए, तो 0 ऐरे के एन्ड वैल्यू को रिटर्न करता है।
  • Recursive Case – इसी प्रकार नेक्स्ट इंडेक्स के साथ रिकर्सिव फंक्शन कॉल के रिज़ल्ट में मौजूदा एलिमेंट को ऐड करते है।

Example of reversing a string using the recursion method in Java.

यहाँ जावा प्रोग्रामर किसी टेक्स्ट स्ट्रिंग को रिवर्स करने के लिए रिकर्सन मेथड फंक्शन को यूज़ कर सकते हैं।

Example of the recursive string reversal method in Java.

public class Main

{

// here we use Recursive function method to reverse a text string

    public static String reverseString(String string) {

        if (string.isEmpty()) {

            return string;  // here we define Base case, if the given string is empty, it will return it

        } else {

            return reverseString(string.substring(1)) + string.charAt(0);  // here we use Recursive case method

        }

    }

    public static void main(String[] args) {

        String output = reverseString(“Vcanhelpsu”);

        System.out.println(“\n the Reverse string is – ” + output);  // Output is – usplehnacV

    }

}

Explanation of the recursive string reversal method.

  • यहाँ हमने इस प्रोग्राम में हमने reverseString फंक्शन डिफाइन किया है.
  • Base case – यहाँ यदि टेक्स्ट स्ट्रिंग एम्प्टी है, तो यह स्ट्रिंग टर्मिनेशन कंडीशन को रिटर्न करें।
  • Recursive case – यह reverseString मेथड में अपने आप को सबस्ट्रिंग (फर्स्ट कैरेक्टर को छोड़कर) के साथ स्टिंग को कॉल करता है, और अंत में पहला कैरेक्टर को ऐड कर डिस्प्ले करता है।

Leave a Reply