Recursive method with example
Skip to main content

Recursive method with example

 recursion,recursive java,recursive program in java, recursive java example, fibonacci recursive java, java recursive method, recursive function java,java program to print odd number using recursion,print numbers from 1 to n by using recursion,using recursive method,asterisk pyramid using recursive function in python,star pyramid using recursive function in python,how to generate patterns using recursive functions,java program using recursion,write a recursive function to print first n odd natural numbers





Recursive method with example:-

Write a Program to print first five characters using recursive method in Java ?

Recursion
 :- Java is support the mechanism of recursion.  Recursion is the process of defining something in terms of itself. As it relates to Java programming, recursion is the attribute that allows a method to call itself. The method is call it self process is called recursion.

Example: In this example we create a RecursionExam and inside this class we create a method PrintFiveChar(). In this method with return type of string. We doing in this a methodology to print a first five charcter of the string and return a String type value. After that we create also a Main class there we take a in put from the user by using of a Scanner class and there we create a object of the RecursionExam class print the first five characters of the String that user insert using System.out.println().

class RecursionExam{
      
       String PrintFiveChar(String Svalue){
            int n=4;
           String mFirstFiveChar;
            if(Svalue.length()>n){
            mFirstFiveChar = Svalue.substring(0,n);
            }else{
             mFirstFiveChar=Svalue;
           }
          return mFirstFiveChar;
        }         
    }
class Main{
      public static void main(String [] args){
          Scanner s = new Scanner(System.in);
         System.out.println("Enter String : ");
          String getString = s.nextLine();
          RecursionExam mRecur = new RecursionExam();
         String mFiveChar = MRecur.PrintFiveChar(getString);
      System.out.println("First five characters of string is : " +mFiveChar);
    }
}

Post a Comment

0 Comments