Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
Problem Statement - Repeating a character using loop
Write a program to repeat the character as given by the user for number of times as desired by the user. Show the repeated character in the same line. Use for loop for repeatition.
Alternate post is a link to another way of doing the program which results in same output but with a different programming approach.
:
Run Output
Give a single character : #
Give repetition count : 5
#####
char c; //Storage variable as char type for single character to be given by user int rc; //Storage variable for repetition count to be given by the user. int type cout<<“Give a single character : “; cin>>c;
Collecting user input for character.
cout<<“Give repetition count : “; cin>>rc;
Collecting user input for repetition count
for(int i=0;i<rc;++i) cout<<c;
Loop runs till rc and prints the character in c. As there is no additional character in the cout chain, the symbol is printed repeatedly in the same line.
Common Errors
Some students make error in building the loops examples are. for(int i=0;i<=rc,++i) will print one additional time as the loop begins from 0. for(int i=1;i<rc,++i) will print 1 less time as loop starts from 1 and ends at less than count.Correct options are: for(int i=0;i<rc,++i) for(int i=1;i<=rc,++i)
The first one out of these two options is preferable as comparison is just less than and not less than and equal to.