How will you print numbers from 1 to 100 without using loop in C?

CProgrammingServer Side Programming

There are several methods to print numbers without using loops like by using recursive function, goto statement and creating a function outside main() function.

Here is an example to print numbers in C language,

Example

 Live Demo

#include<stdio.h>
int number(int val) {
   if(val<=100) {
      printf("%d\t",val);
      number(val+1);
   }
}
int main() {
   int val = 1;
   number(val);
   return 0;
}

Output

12345678910111213 14151617181920212223242526 27282930313233343536373839 40414243444546474849505152 53545556575859606162636465 66676869707172737475767778 79808182838485868788899091 9293949596979899100

In the above example, a function number is created with an argument val. If val is less or equal to 100, print the value and increment value by one. In the main() function, val is initialized with 1 and called function number.

if(val<=100) {
   printf("%d\t",val);
   number(val+1);
}
raja
Published on 05-Nov-2018 12:18:04
Advertisements