In: Computer Science
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. Need code in c#.
Thanks for the question, here is the recursive program
================================================================
using System;
class Driver {
public static string repeatNTimes(String s, int n){
// base case
if(n<=0)return "";
// call recursively
else return s + repeatNTimes(s,n-1);
}
public static void Main (string[] args) {
Console.WriteLine (repeatNTimes("hello",3));
Console.WriteLine (repeatNTimes("hello",7));
}
}
==================================================================