Open In App

How to print N times without using loops or recursion ?

Last Updated : 12 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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;
}


Python3




class Print:
    def __init__(self):
        print("Hello")
 
N = 5
a = [Print() for i in range(N)]


Output:

Hello
Hello
Hello
Hello
Hello

Time Complexity: O(N) 

Auxiliary Space: O(N)

Extra space is required for array initialisation.


Similar Reads

Sum of array Elements without using loops and recursion
Given an array of N elements, the task is to find the Sum of N elements without using loops(for, while &amp; doWhile) and recursion.Examples: Input: arr[]={1, 2, 3, 4, 5} Output: 15 Input: arr[]={10, 20, 30} Output: 60 j Approach: Unconditional Jump Statements can be used to solve this problem.Unconditional Jump Statements:Jump statements interrupt
5 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 Concept of setjump and longjump in C. C/C++ Code //
1 min read
C++ Program to Print an Array using Recursion
Write a program in C++ to print an Array using Recursion 1. Using Static Variable Static variables have the property of preserving their value even after they are out of their scope! Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope.Syntax: static data_type var_name = var_va
2 min read
C++ Program To Print Reverse of a String Using Recursion
Write a recursive function to print the reverse of a given string. Code:  C/C++ Code // C++ program to reverse a string using recursion #include &lt;bits/stdc++.h&gt; using namespace std; /* Function to print reverse of the passed string */ void reverse(string str) { if(str.size() == 0) { return; } reverse(str.substr(1)); cout &lt;&lt; str[0]; } /*
2 min read
Times required by Simple interest for the Principal to become Y times itself
Given that a certain amount of money becomes T1 times itself in N1 years. The task is to find the number of years i.e. N2 so that the amount becomes T2 times itself at the same rate of simple interest. Examples: Input: T1 = 5, N1 = 7, T2 = 25 Output: 42 Input: T1 = 3, N1 = 5, T2 = 6 Output: 12.5 Approach: Let us consider the 1st Example where T1 =
3 min read
Print Number series without using any loop
Problem - Givens Two number N and K, our task is to subtract a number K from N until number(N) is greater than zero, once the N becomes negative or zero then we start adding K until that number become the original number(N). Note : Not allow to use any loop.Examples : Input : N = 15 K = 5 Output : 15 10 5 0 1 5 10 15 Input : N = 20 K = 6 Output : 2
6 min read
C/C++ program to print Hello World without using main() and semicolon
The task is to write a program to print Hello World without using main() and semicolon. As we already know, how to print Hello World without the use of a semicolon. Now, for writing without main() method, we will need a Macro. C/C++ Code // C program to print Hello World // without using main() and semicolon #include &lt;stdio.h&gt; #define x main
1 min read
Print all array elements appearing more than N / K times
Given an array arr[] of size N and an integer K, the task is to find all the array elements that appear more than (N / K) times. Examples: Input: arr[] = { 1, 2, 6, 6, 6, 6, 6, 10 }, K = 4Output: 6Explanation: The frequency of 6 in the array is greater than N / K(= 2). Therefore, the required output is 6. Input: arr[] = { 3, 4, 4, 5, 5, 5, 5 }, K =
21 min read
C++ Program to Print array after it is right rotated K times
Given an Array of size N and a values K, around which we need to right rotate the array. How to quickly print the right rotated array?Examples :   Input: Array[] = {1, 3, 5, 7, 9}, K = 2. Output: 7 9 1 3 5 Explanation: After 1st rotation - {9, 1, 3, 5, 7} After 2nd rotation - {7, 9, 1, 3, 5} Input: Array[] = {1, 2, 3, 4, 5}, K = 4. Output: 2 3 4 5
2 min read
Find the value of ln(N!) using Recursion
Given a number N, the task is to find the log value of the factorial of N i.e. log(N!).Note: ln means log with base e.Examples: Input: N = 2 Output: 0.693147 Input: N = 3 Output: 1.791759 Approach:Method -1: Calculate n! first, then take its log value.Method -2: By using the property of log, i.e. take the sum of log values of n, n-1, n-2 ...1. ln(n
3 min read
Article Tags :