Given a character c and a number n, print the character c, n times. We are not allowed to use loop, recursion and goto.
Input : n = 10, c = 'a' Output : aaaaaaaaaa Input : n = 6, c = '\n' Output : Input : n = 6, character = '@' output : @@@@@@
In C++, there is a way to initialize a string with a value. It can be used to print a character as many times as we want. 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 print a particular character and the other is the character itself.
Below is the implementation which illustrates this.
Another Method: As we know that every time an object of a class is created the constructor of that class is called we can use it to our advantage and print the character inside the constructor, and create N objects of that class.
@ @ @ @ @ @
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print a number 100 times without using loop, recursion and macro expansion in C?
- Print 1 to 100 in C++, without loop and recursion
- How to print N times without using loops or recursion ?
- Change/add only one character and print '*' exactly 20 times
- How will you print numbers from 1 to 100 without using loop?
- How will you print numbers from 1 to 100 without using loop? | Set-2
- Print pattern using only one loop | Set 1 (Using setw)
- C++ Program to print an Array using Recursion
- goto statement in C/C++
- Check input character is alphabet, digit or special character
- C/C++ while loop with Examples
- C/C++ For loop with Examples
- for_each loop in C++
- C/C++ do while loop with Examples
- Range-based for loop in C++
- Difference between while and do-while loop in C, C++, Java
- Different types of range-based for loop iterators in C++
- What happens if loop till Maximum of Signed and Unsigned in C/C++?
- Reversed Range-based for loop in C++ with Examples
- Solving f(n)= (1) + (2*3) + (4*5*6) ... n using Recursion
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Blinkii