Print 1-10 numbers without using Conditional Loop
Advertisements
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
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
Google Advertisment