
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print Numbers from 1 to 100 Without Using Loop in C
You can print the numbers from 1 to 100 without a loop by using the various methods like recursive function and goto statement that print the list of integers.
The following are the approaches to print the numbers from 1 to 100:
Using Recursion
As we know, recursion is the process of calling the function itself. Here, we use the recursive function to accept an integer and set the iteration of plus 1 without the logic of loop and get the expected outcome.
Example
In this example, we use an if statement to check whether the given integer is less than or equal to 100. If the condition is true, the current number is printed, and the function calls itself recursively with the next number (n + 1).#include <stdio.h> void printNumbers(int n) { if (n > 100) return; printf("%d
", n); printNumbers(n + 1); } int main() { printNumbers(1); return 0; }
Output
The above program produces the following result:
1 2 ... ... ... 100
Using goto Statement
In the below program, we use a goto statement to repeatedly execute a block of code. An if statement checks whether the value of i is less than or equal to 100. If true, it prints the number, increments i, and jumps back to the start to continue the process until the condition becomes false.
Example
Following is an example of goto statement to generate the list of integers between 1 and 100.
#include <stdio.h> int main() { int i = 1; start: if (i <= 100) { printf("%d
", i); i++; goto start; } return 0; }
The above program produces the following result:
1 2 ... ... ... 100