Skip to content
Related Articles
Open in App
Not now

Related Articles

How to print N times without using loops or recursion ?

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 07 Jul, 2022
Improve Article
Save Article

How to print “Hello” N times (where N is user input) without using loop or recursion or goto.

Input : N, that represent the number of times you want to print the statement. Output : Statement for N times

First, we create a class. After that, we need to initialize the constructor of the class by writing the statement you want to print inside a cout/print statement. The basic idea used here that “The no. of times you create the object of the class, the constructor of that class is called that many times.” 

CPP




// CPP program to print a sentence N times
// without loop and recursion.
// Author : Rohan Prasad
#include <iostream>
using namespace std;
class Print {
 
public:
  Print()
  {
    cout << "Hello" << endl;
  }
};
 
int main()
{
  int N = 5;
  Print a[N];
  return 0;
}

Output:

Hello
Hello
Hello
Hello
Hello

Time Complexity: O(N) 
Auxiliary Space: O(1), no extra space is required, so it is a constant.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!