Print 1-10 numbers without using Conditional Loop

Google Advertisements

Prev Tutorial Next Tutorial

Print 1 to 10 numbers without using Conditional Loop in C

Print 1 to 10 numbers without using Conditional Loop means without using, for Loop, while Loop and do-while Loop. Below all these are conditional statement in C Programming Language.

  • for Loop
  • while Loop
  • do-while Loop

Above code can be achieved in 3 ways, which is given below.

  • Using Printf Statement 10 Times.
  • Using Recursive Function
  • Using goto Statement.
  • Recursive Main Function

Using printf function print 1, 2, 3, 4.................10 one by one you can see in belwo example.

Write Printf Statement 10 times

#include<stdio.h>
#include<conio.h>

void main()
{
printf("1");
printf("2");
printf("3");
printf("4");
printf("5");
printf("6");
printf("7");
printf("8");
printf("9");
printf("10");
getch();
}

Output

1
2
3
4
5
6
7
8
9
10
Using Recursive Function

Recursive function Call function Itself. In below code Print-1-10-Number(int value) function calls itself so it is called Recursive function

Printf Statement 10 Using Recursive Function

#include<stdio.h>
#include<conio.h>

void Print-1-10-Number(int value)
{
 int i;
 printf("%d\n", value);
 i = value + 1;

 if (i>10)
 return;
 Print-1-10-Number(i);
}
 
void main() 
{
   Print-1-10-Number(1);
}
getch();
}

Output

1
2
3
4
5
6
7
8
9
10

Using Goto Statement

#include<stdio.h>
#include<conio.h>

void main()
{
  int i = 0;
  Start: i = i + 1;
  printf("%d", i);
 
  if (i <= 10)
  goto Start;
  getch();
}

Output

1
2
3
4
5
6
7
8
9
10

Using Recursive Main

#include<stdio.h>
#include<conio.h>

 void main() 
 {
  static int i = 1;
  if (i <= 10) 
  {
   printf("%d", i++);
   main();
  }
 getch();
}

Static variable inside a function means once the variable has been initialized, it remains in memory until the end of the program

Output

1
2
3
4
5
6
7
8
9
10

Prev Tutorial Next Tutorial

Google Advertisements

We have more than 5 Groups which have 500000+ Members and some Pages have more than 100000+ members. For Advertisement contact on [email protected] or 8076671483

Buy This Ad Space @$50 per Month, Ad Size 600X200 Contact on: [email protected] or 8076671483

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Classpath variable is set for providing a path for predefined Java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar. Difference between path and classpath in Java

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user. Cloud Computing Tutorial

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects. Download Java C C++ Projects

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: [email protected] or 8076671483 Try this Keyword C++ Programs