C Program to print counting without any loops


      This is also a challenging and hot question asked in interview. This technique uses the concepts of recursion and main function.

Program:

#include <stdio.h>

int main()
{
static int i;
i++;
if(i==101)
return 0;
else
{
printf("%d\n",i);
main();
}
return 0;
}

Output:


Logic:

      We have declared an integer variable of static type, which has a special property that each time the control goes outside the function scope, these variables holds their state and values. In next step, we have called the main function itself repeatedly until the value of static int meets till the desired values. The main function recursively iterate over and over again till condition is satisfied. As soon as condition is satisfied, control jumps out the main function.

Comments

Popular Posts