Print all numbers between 1 to N without using semicolon

Write a program to print all numbers between 1 to N without using semicolon.

 


 
Method 1: (Using static variable in recursive main())

We can call main() function recursively and with each call we print next element from the series. To store information about previous element printed, we use a static variable (Note that a global variable will also work fine).

C++ implementation –
 

 
 
Method 2: (Using while loop)

We can also print the series iteratively by using a while loop. With each iteration of the while loop, we print an element. To avoid using semicolons, we use cout inside while loop or IF statement conditional expression.

C++ implementation –
 

OR

 

Can we print in opposite order from N to 1 without using semicolon?

Sure we can. Here we go –
 

OR

 
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)

Loading...

Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂
 



Leave a Reply

avatar
  Subscribe  
newest oldest most voted
Notify of
uwu
Guest

def printN(n: Int): Unit = (1 to n).foreach(println)