Print numbers from 1 to 100 Using recursive-main
Follow the steps mentioned below to implement the recursive main:
- declare variable i of value 1.
- keep calling the main function till i < 100.
Below is the implementation of the above approach:
C
// C program to count all pairs from both the // linked lists whose product is equal to // a given value #include <stdio.h> int main() { static int i = 1; if (i <= 100) { printf ( "%d " , i++); main(); } return 0; } |
C++
// C++ program to count all pairs from both the // linked lists whose product is equal to // a given value #include <iostream> using namespace std; int main() { static int i = 1; if (i <= 100) { cout << i++ << " " ; main(); } return 0; } // This code is contributed by ShubhamCoder |
Java
// Java program to count all pairs from both the // linked lists whose product is equal to // a given value class GFG { static int i = 1 ; public static void main(String[] args) { if (i <= 100 ) { System.out.printf( "%d " , i++); main( null ); } } } // This code is contributed by Rajput-Ji |
Python3
# Python3 program to count all pairs from both # the linked lists whose product is equal to # a given value def main(i): if (i < = 100 ): print (i, end = " " ) i = i + 1 main(i) i = 1 main(i) # This code is contributed by SoumikMondal |
C#
// C# program to count all pairs from both the // linked lists whose product is equal to // a given value using System; class GFG { static int i = 1; public static void Main(String[] args) { if (i <= 100) { Console.Write( "{0} " , i++); Main( null ); } } } // This code is contributed by Rajput-Ji |
Javascript
// Program to count all pairs from both //the linked lists whose product is equal to // a given value function main(i) { if (i <= 100) { console.log(i + " " ); i = i + 1; main(i); } } let i = 1; main(i); |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
Time complexity: O(1)
Auxiliary Space: O(1)
Print 1 to 100 without loop using Goto and Recursive-main
Our task is to print all numbers from 1 to 100 without using a loop. There are many ways to print numbers from 1 to 100 without using a loop. Two of them are the goto statement and the recursive main.