- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print a character n times without using loop, recursion or goto in C++
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.
Example Code
#include <iostream> using namespace std; void print_char_n_times(char my_char, int count) { cout << string(count, my_char) << endl; } int main() { //print character B 10 times print_char_n_times('B', 10); //print character x 30 times print_char_n_times('x', 30); }
Output
BBBBBBBBBB xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Advertisements