Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that calls this method from the Main program.
Program should ask for input and be stored in the string and should return n amount of times.
HERE IS THE CODE...PLEADE GIVE AN UPVOTE
import java.io.*;
import java.util.Scanner;
public class Main {
// class declaration
public static String repeat(String str,int n)
{
if (n==1)
return str;
else
return str+repeat(str,(n-1));
}
public static void main(String args[]){
System.out.println("please enter the string and number of times");
Scanner sc= new Scanner(System.in);
String str;
int n;
str= sc.next();
n=sc.nextInt();
System.out.println("result : " +repeat(str,n)); // print the
result
}
}
INPUT ----> String mon n=4
result : monmonmonmon
Get Answers For Free
Most questions answered within 1 hours.