Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on Recursion
  • Practice Recursion
  • MCQs on Recursion
  • Recursion Tutorial
  • Recursive Function
  • Recursion vs Iteration
  • Types of Recursions
  • Tail Recursion
  • Josephus Problem
  • Tower of Hanoi
  • Check Palindrome
Open In App
Next Article:
How to print a number 100 times without using loop and recursion in C?
Next article icon

Print 1 to 100 without loop using Goto and Recursive-main

Last Updated : 06 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Print numbers from 1 to 100 Using Goto statement 

Follow the steps mentioned below to implement the goto statement:

  • declare variable i of value 0
  • declare the jump statement named begin, which increases the value of i by 1 and prints it.
  • write an if statement with condition i < 100, inside which call the jump statement begin

Below is the implementation of the above approach:

C++




#include <iostream>
using namespace std;
 
int main()
{
    int i = 0;
 
begin:
    i = i + 1;
    cout << i << " ";
 
    if (i < 100) {
        goto begin;
    }
    return 0;
}
 
// This code is contributed by ShubhamCoder
 
 

C




#include <stdio.h>
 
int main()
{
    int i = 0;
begin:
    i = i + 1;
    printf("%d ", i);
 
    if (i < 100)
        goto begin;
    return 0;
}
 
 

Java




public class Main {
    public static void main(String[] args) {
        int i = 0;
 
        while (i < 100) {
            i = i + 1;
            System.out.print(i + " ");
        }
    }
}
 
 

C#




using System;
 
class GFG {
 
    static public void Main()
    {
        int i = 0;
    begin:
        i = i + 1;
        Console.Write(" " + i + " ");
 
        if (i < 100) {
            goto begin;
        }
    }
}
 
// This code is contributed by ShubhamCoder
 
 

Python3




i = 0
 
while i < 100:
    i = i + 1
    print(i, end=' ')
 
 

Javascript




// Javascript
let i = 0;
 
while (i < 100) {  // loop will iterate 100 times
    i += 1;
    console.log(i, end=' '); // print i and add a space
}
 
 
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 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)



Next Article
How to print a number 100 times without using loop and recursion in C?

P

Pushpanjali chauhan
Improve
Article Tags :
  • C Language
  • DSA
  • Recursion
  • cpp-puzzle
Practice Tags :
  • Recursion

Similar Reads

  • Print 1 to 100 in C++ Without Loops and Recursion
    We can print 1 to 100 without using loops and recursion using three approaches discussed below: 1) Template Metaprogramming: Templates in C++ allow non-datatypes also as parameters. Non-datatype means a value, not a datatype. Example: C/C++ Code // CPP Program to print 1 to 100 // without loops and
    3 min read
  • How to print a number 100 times without using loop and recursion in C?
    It is possible to solve this problem using loop or a recursion method but what if both are not allowed? A simple solution is to write the number 100 times in cout statement. A better solution is to use #define directive (Macro expansion) C/C++ Code // CPP program to print &quot;1&quot; 100 t
    1 min read
  • Print a character n times without using loop, recursion or goto in C++
    Given a character c and a number n, print the character c, n times. We are not allowed to use loop, recursion, and goto. Examples : Input : n = 10, c = 'a'Output : aaaaaaaaaa Input : n = 6, character = '@'Output : @@@@@@ In C++, there is a way to initialize a string with a value. It can be used to p
    2 min read
  • Print a number 100 times without using loop, recursion and macro expansion in C?
    It is possible to solve this problem using loop or a recursion method. And we have already seen the solution using #define directive (Macro expansion) but what if all three are not allowed? A simple solution is to write the number 100 times in cout statement. A better solution is to use concept of C
    1 min read
  • Print 1 to n without using loops
    You are given an integer n. Print numbers from 1 to n without the help of loops. Examples: Input: N = 5Output: 1 2 3 4 5Explanation: We have to print numbers from 1 to 5. Input: N = 10Output: 1 2 3 4 5 6 7 8 9 10Explanation: We have to print numbers from 1 to 10. Approach: Using RecursionTo solve th
    3 min read
  • How will you print numbers from 1 to 100 without using a loop?
    If we take a look at this problem carefully, we can see that the idea of "loop" is to track some counter value, e.g., "i = 0" till "i <= 100". So, if we aren't allowed to use loops, how can we track something in the C language?Well, one possibility is the use of 'recursion', provided we use the t
    12 min read
  • Print a pattern without using any loop
    Given a number n, print the following pattern without using any loop. n, n-5, n-10, ..., 0, 5, 10, ..., n+5, n Examples : Input: n = 16Output: 16, 11, 6, 1, -4, 1, 6, 11, 16 Input: n = 10Output: 10, 5, 0, 5, 10 We strongly recommend that you click here and practice it, before moving on to the soluti
    11 min read
  • Print numbers 1 to N using Indirect recursion
    Given a number N, we need to print numbers from 1 to N with out direct recursion, loops, labels. Basically we need to insert in above code snippet so that it can be able to print numbers from 1 to N? C/C++ Code #include <stdio.h> #define N 20; int main() { // Your code goes Here. } Examples :
    5 min read
  • Print N to 1 without loop
    You are given an integer N. Print numbers from N to 1 without the help of loops. Examples: Input: N = 5Output: 5 4 3 2 1Explanation: We have to print numbers from 5 to 1. Input: N = 10Output: 10 9 8 7 6 5 4 3 2 1Explanation: We have to print numbers from 10 to 1. Approach: If we take a look at this
    3 min read
  • C Program to print numbers from 1 to N without using semicolon?
    How to print numbers from 1 to N without using any semicolon in C. C/C++ Code #include&lt;stdio.h&gt; #define N 100 // Add your code here to print numbers from 1 // to N without using any semicolon What code to add in above snippet such that it doesn't contain semicolon and prints numbers fr
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences