How to print 1-10 numbers without using Conditional Loop in C Programming ?

May 24, 2024 2 Comments » Hits : 576





Print 1-10 numbers without using Conditional Loop i.e without using

  • for Loop
  • while Loop
  • do-while Loop

This can be achieved in 3 ways :

  1. Using Printf Statement 10 Times.
  2. Using Recursive Function
  3. Using goto Statement.
  4. Recursive Main Function

Way 1 : Printf Statement 10 times

#include<stdio.h>
void main()
{
printf("1");
printf("2");
printf("3");
printf("4");
printf("5");
printf("6");
printf("7");
printf("8");
printf("9");
printf("10");
}
  • Use 10 Times Printf Statement .

Way 2 : Recursive Function

#include<stdio.h>
void printNumber(int value)
{
int i;
printf("%d\n",value);
i = value + 1;
if(i>10)
return;
printNumber(i);
}
void main()
{
printNumber(1);
}
  • Recursive function : Calling Itself .
  • printNumber function calls itself so it is called Recursive function .

Way 3 : Using Goto Statement

#include<stdio.h>
void main()
{
int i=0;
Start :
i = i + 1;
printf("%d\n",i);
if(i< =10)
goto Start;
}

Way 4 : Using Recursive Main

#include<stdio.h>
main()
{
static int i=1;
   if(i<=10)
   {
   printf("%d\n",i++);
   main();
   }
}
  • Static variable inside a function means “once the variable has been initialized, it remains in memory until the end of the program”

Incoming search terms:

  • abhinav narain

    write assembly level code for this.

  • ROY

    THIS IS REALLY INFORMATIVE THANKS