repeat character n times c++
To repeat a character n
times in C++, you can use a loop to iterate n
times and print the character each time. Here are the steps to achieve this:
- Initialize a variable
n
with the desired number of repetitions. - Initialize a variable
c
with the character you want to repeat. - Use a
for
loop to iteraten
times. - Inside the loop, print the character
c
.
Here is the code snippet that demonstrates this:
#include <iostream>
int main() {
int n = 5;
char c = '*';
for (int i = 0; i < n; ++i) {
std::cout << c;
}
return 0;
}
In this example, the character '*'
is repeated 5 times. You can modify the values of n
and c
to repeat a different character or a different number of times.
When you run this code, it will output , which is the character
''
repeated 5 times.