
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C Program to print numbers from 1 to N without using semicolon
Here we will see a tricky solution of the problem. We shall print some numbers from 1 to N without using any semicolon.
We can solve this problem in two different methods. The first one is the Iterative method, and second one is recursive method.
Method 1
The printf() function returns the length of the string so it is a non-zero value. We can perform logical AND with the condition to print the result. Then increase the value of the counter.
Example Code
#include<stdio.h> #define N 20 int main(int num, char *argv[]) { while (num <=N && printf("%d ", num) && num++) { //The while body is empty. } }
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Method 2
In the second method we will see how to do the same task using recursion. We will pass some argument with the main function. This main will be called recursively.
Example Code
#include<stdio.h> #define N 20 main(int val) { if (val <=N && printf("%d ", val) && main(val + 1)) { //Body is empty } }
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
- Related Articles
- C Program to print “Hello World!” without using a semicolon
- How to print a semicolon(;) without using semicolon in C/C++?
- Program to print numbers from 1 to 100 without using loop
- Write a C program to print “ Tutorials Point ” without using a semicolon
- Write a program to print ‘Tutorials Point’ without using a semicolon in C
- How will you print numbers from 1 to 100 without using loop in C?
- Program to print ‘N’ alphabet using the number pattern from 1 to n in C++
- Print Hello World without semicolon in C++
- Python Program to Print Numbers in a Range (1,upper) Without Using any Loops
- Golang Program to Print the Numbers in a Range (1, upper) without Using any Loops
- Print prime numbers from 1 to N in reverse order
- How to print all the Armstrong Numbers from 1 to 1000 using C#?
- C program to print characters without using format specifiers
- Program to print root to leaf paths without using recursion using C++
- Maximum XOR using K numbers from 1 to n in C++

Advertisements