Print a character n times without using loop, recursion or goto in C++ Last Updated : 05 Dec, 2022 Comments Improve Suggest changes Like Article Like Report Given a character c and a number n, print the character c, n times. We are not allowed to use loop, recursion, and goto. Examples : Input : n = 10, c = 'a'Output : aaaaaaaaaa 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. CPP // CPP Program to print a character // n times without using loop, // recursion or goto #include<bits/stdc++.h> using namespace std; // print function void printNTimes(char c, int n) { // character c will be printed n times cout << string(n, c) << endl; } // driver code int main() { // no of times a character // need to be printed int n = 6; char c = 'G'; // function calling printNTimes(c, n); return 0; } OutputGGGGGG Time Complexity: O(1)Auxiliary Space: O(1) 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. CPP #include<bits/stdc++.h> using namespace std; class Geeks{ public: Geeks(){ cout<<"@ "; } }; int main(){ int N =6; Geeks obj[N]; return 0; } Output@ @ @ @ @ @ Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Print a character n times without using loop, recursion or goto in C++ U UPENDRA BARTWAL, Follow Improve Article Tags : Misc C++ cpp-string cpp-puzzle Practice Tags : CPPMisc Similar Reads How to print a number 100 times without using loop and recursion in C? It is possible to solve this problem using loop or a recursion method but what if both are not allowed? A simple solution is to write the number 100 times in cout statement. A better solution is to use #define directive (Macro expansion) CPP // CPP program to print "1" 100 times. // Prints 1 min read Print a number 100 times without using loop, recursion and macro expansion in C? It is possible to solve this problem using loop or a recursion method. And we have already seen the solution using #define directive (Macro expansion) but what if all three are not allowed? A simple solution is to write the number 100 times in cout statement. A better solution is to use concept of C 1 min read Print 1 to 100 without loop using Goto and Recursive-main Our task is to print all numbers from 1 to 100 without using a loop. There are many ways to print numbers from 1 to 100 without using a loop. Two of them are the goto statement and the recursive main. Print numbers from 1 to 100 Using Goto statement Follow the steps mentioned below to implement the 5 min read Print 1 to 100 in C++ Without Loops and Recursion We can print 1 to 100 without using loops and recursion using three approaches discussed below: 1) Template Metaprogramming: Templates in C++ allow non-datatypes also as parameters. Non-datatype means a value, not a datatype. Example: CPP // CPP Program to print 1 to 100 // without loops and recursi 3 min read How will you print numbers from 1 to 100 without using a loop? If we take a look at this problem carefully, we can see that the idea of "loop" is to track some counter value, e.g., "i = 0" till "i <= 100". So, if we aren't allowed to use loops, how can we track something in the C language?Well, one possibility is the use of 'recursion', provided we use the t 12 min read Print individual digits as words without using if or switch Given a number, print words for individual digits. It is not allowed to use if or switch.Examples: Input: n = 123 Output: One Two Three Input: n = 350 Output: Three Five Zero We strongly recommend you to minimize your browser and try this yourself first. The idea is to use an array of strings to sto 5 min read Find the duplicate characters in a string in O(1) space Given a string str, the task is to find all the duplicate characters present in a given string in lexicographical order without using any additional data structure. Examples: Input: str = "geeksforgeeks" Output: e g k s Explanation: Frequency of character 'g' = 2 Frequency of character 'e' = 4 Frequ 10 min read Iterate over characters of a string in C++ Given a string str of length N, the task is to traverse the string and print all the characters of the given string. Examples: Input: str = "GeeksforGeeks" Output: G e e k s f o r G e e k s Input: str = "Coder" Output: C o d e r Naive Approach: The simplest approach to solve this problem is to itera 3 min read String with k distinct characters and no same characters adjacent Given n and k, print a string that has n characters. The string should have exactly k distinct characters and no adjacent positions. Examples: Input : n = 5, k = 3 Output : abcab Explanation: 3 distinct character a, b, c and n length string. Input: 3 2 Output: aba Explanation: 2 distinct character ' 6 min read Change/add only one character and print '*' exactly 20 times In the below code, change/add only one character and print '*' exactly 20 times. int main() { int i, n = 20; for (i = 0; i < n; i--) printf("*"); getchar(); return 0; } Solutions:1. Replace i by n in for loop's third expression C++ #include <iostream> using namespace std; int main() { int i 5 min read Like