Sample C Program to Print 1 to 100

How to write a Sample C Program to Print 1 to 100 without using For Loop, Do While, and While Loop with an example.

Sample C Program to Print 1 to 100 without using Loop

In the example, we are going to write a Program to Print 1 to 100 without using Loops.

/* Sample C Program to Print 1 to 100 without Loop */

#include<stdio.h>
int print (int number);
int main()
{
    int num = 1;
    print(num);
    return 0;
}
int print (int number)
{
    if(number <= 100)
    {
       printf("%d\t", number);
       print(number + 1); // Calling Function recursively
    }
}
Sample C Program to Print 1 to 100 without Loop 1

Within this Program to Print 1 to 100 without loops example, When the compiler reaches to print(num) line in the main() program, then the compiler will immediately jump to below function:

int print (int number)

Within the function, we used the If Statement to check the number is less than or equal to 100 or not. If the condition returns TRUE, then statements inside the If will be executed.

if(number <= 100)
  {
     printf("%d\t", number);
     print(number + 1); // Calling Function recursively
  }

Within the If block of this Program, we used the print(number + 1) statement, which will help to call the function Recursively with the updated value. If you miss this statement, then after completing the first line, it will terminate, and the output will be 1. Please refer to Recursion in C article to understand the recursive function. And also refer to For Loop, Do While, and While Loop in C programming.