GoogleTag

Google Search

[Programming] Question 29: Write a Program in C to Print all natural numbers up to N without using a semi-colon

#include <stdio.h>

#define N 10

int main() {
	if(printf("1\n")) {}
#define PRINT(N) if(printf("%d\n", N) && N < 10) PRINT(N+1)
PRINT(2);
	return 0;
} 

 

Inside the main() function:

- The program uses preprocessor directives to define the value of `N` as 10.

- The first natural number, 1, is printed using the `printf` function inside the `if` statement.

- A macro `PRINT(N)` is defined to recursively print natural numbers from 2 up to `N` without using a semi-colon.

- The macro `PRINT(N)` calls itself with incremented value of `N` if `N` is less than 10.

- The program then calls the `PRINT` macro with the initial value of 2 to start printing natural numbers.

 

Featured Posts

SQL Interview Questions Topics

 SQL Topics to prepare for interviews,   SQL Basics: Introduction to SQL SQL Data Types DDL (Data Definition Language): C...

Popular Posts