## Print a String n times using recursion

Related searches

I am having trouble with the following excersice.

Write a method starString that accepts an integer parameter n and returns a string of stars (asterisks) 2n long (i.e., 2 to the nth power). For example:

```Call    Output  Reason
starString(0);  "*"                    2^0 = 1
starString(1);  "**"                    2^1 = 2
starString(2);  "****"                  2^2 = 4
starString(3);  "********"          2^3 = 8
starString(4);  "****************"  2^4 = 16
```

You should throw an IllegalArgumentException if passed a value less than 0.

I have tried this, but I am looking for a way to multiply the String "*", several times. PD: I can't use .repeat

I am stuck on getting the value of 2 to the n power. As i have tried

```int x = (int)Math.pow(2,(n));
```

but i don't know what to do with that.

```public static String starString(int n) {
if(n<0){
throw new IllegalArgumentException("No negative values allowed");
}
if(n==0){
return "*";
}else{

return starString(0) + starString(n-1);
}

}
```
```test #1:starString(0)
return:"*"
result: pass

test #2:starString(1)
return:"**"
result: pass

test #3:starString(2)
expected return:"****"
result: fail

test #4:starString(3)
expected return:"********"
result: fail

test #5:starString(4)
expected return:"****************"
result: fail

test #6:starString(-1)
exp. exception:IllegalArgumentException
your exception:IllegalArgumentException on line 3: No negative values allowed
result: pass
```

This will work, although it will blow up for large n.

```public static String starString(int n) {
if(n<0){
throw new IllegalArgumentException("No negative values allowed");
}
if(n==0){
return "*";
}else{

return starString(n-1) + starString(n-1);
}

}
```

obviously you can get 2^n using

```public static int twoToN(int n) {
if(n<0){
throw new IllegalArgumentException("No negative values allowed");
}
if(n==0){
return 1;
}else{

return 2* twoToN(n-1);
}
}
```

Print a String 'X' Times (No Loop), You can use recursion like this private void printStar(int n){ if(n > 0){ System.out. print("*"); printStar(n-1); } }. And call the method like this initially - printStar(4);. How to print N times without using loops or recursion ? Print a character n times without using loop, recursion or goto in C++; Print a number 100 times without using loop, recursion and macro expansion in C? What is the best way in C to convert a number to a string? Program to compute Log n

You could do something like this

```public static void main(String[] args) {
int count = (int)Math.pow(2, 4);
System.out.println(count);
String value = starString(count);

System.out.println(value);
System.out.println(value.length());

}

static String starString(int n) {
String result = "";
if (n > 0) {
result =  "*" + starString(n -1);
}
return result;
}
```

Print a character n times without using loop, recursion or goto in C++ , While declaring a string, it can be initialized by using the feature provided by c++. It takes 2 arguments. First is the number of times we want to� Is it possible to print a string 'x' times? For example if given the string . String q = "*"; Let's say the user entered the number '4' for the amount of times that they wanted the string repeated. The program would print: ****

Try this :

```     public static String starString(int n) {
if (n < 0) {
throw new IllegalArgumentException("No negative values allowed");
}
if (n == 0) {
return "*";
} else {
int pow = (int) Math.pow(2, n);
int pow2 = pow / 2;
int starsCount = pow - pow2;
StringBuilder stringBuilder = new StringBuilder();

IntStream.iterate(0, operand -> operand + 1)
.limit(starsCount)
.forEach(value -> stringBuilder.append("*"));

return stringBuilder.toString() + starString(n - 1);
}
}
```

In every recursive step I count how many stars have to be appended `(pow - pow2)`.

How to write a recursive method in Java to print a string n times , void print(int n) {. if(n>1) {. print(n-1);. } System.out.println("string");. } this is a task that should be done with a loop though. 25 views. Mario Ray Mahardhika� In this section we will see how to print a character n times without using loops and recursion in C++. We can solve this problem by using string class constructors. There is a constructor where we are taking the character that will be printed multiple times, and the number of times it will be printed.

An important optimization that can help with expensive function calls such as this is memoization; if you start with a table containing one star for the case of zero and then populate the table as you return output(s) the output does not need to be re-computed for every step. Like,

```private static Map<Integer, String> memo = new HashMap<>();
static {
memo.put(0, "*");
}

public static String starString(int n) {
if (n < 0) {
throw new IllegalArgumentException("No negative values allowed");
} else if (!memo.containsKey(n)) {
final String pre = starString(n - 1);
StringBuilder sb = new StringBuilder(pre.length() * 2);
sb.append(pre).append(pre);
memo.put(n, sb.toString());
}
return memo.get(n);
}
```

Then you can print each line (and count the stars) like

```public static void main(String[] args) {
for (int i = 0; i < 8; i++) {
String s = starString(i);
System.out.printf("%d: %s%n", s.length(), s);
}
}
```

Which outputs

```1: *
2: **
4: ****
8: ********
16: ****************
32: ********************************
64: ****************************************************************
128: ********************************************************************************************************************************
```

Repeat a string repeat a string using recursion, Sometimes, when I press ctrl+enter it will pass the first test, and then other times, with no changes to the script, it won't. Here's my code : var out = '� C program to print your name 10 times without using any loop or goto statement In this program, we will learn how to print any message without using any loop or goto statement? . Here we will use recursion – recursion is a process by which a function calls itself .

Three ways to repeat a string in JavaScript, using recursion; using ES6 repeat() method. The Algorithm Challenge Description. Repeat a given string (first argument) num times (second� (Oct-23-2019, 01:35 PM) ichabod801 Wrote: You need to get rid of the loop, just print one time per function call. Then you need a terminating condition that stops the recursion. You would typically pass the value that terminates as a parameter to the function, and modify it each time you make the recursive call. Please share code for this

Learn to repeat a given string N times, to produce a new string which contains all the repetitions, though a simple Java program. We will use method Sting.repeat(N) (since Java 11) and using regular expression which can be used till Java 10.

Recursion continues this way, when pointer reaches ‘\0’, all functions accumulated in stack print char at passed location (str) and return one by one. Time Complexity: O(n) See Reverse a string for other methods to reverse string.