Recursive method with example:-
Write a Program to print first five characters using recursive method in Java ?
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);
}
}
0 Comments
Hello Friends, Please comment and share and do not write spam messages