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

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂