How to print 1 to 100 numbers without using loop. In my previous coding exercise, i have written a code to print 1 to 100 numbers using for and while loop.
To print a number from 1 to 100, we use recursion.
To print a number from 1 to 100, we use recursion.
Program to Print 1 to 100 without using Loop
#include <stdio.h>
int main(){
/* Initialize num with 1 . */
int num = 1;
/* Call printnumber function. */
printnumber(num);
return 0;
}
int printnumber(num){
/* If value of num is less than equal to 100. */
if(num<=100){
printf("%d ",num);
/* Recursively call printnumber with plus one in last num value. */
printnumber(num+1);
}
}
C Practice Questions
No comments:
Post a Comment