• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

Write a C Program to Print 1 to 100 without Loop, Recursion or Goto

Aniruddha Chaudhari/21852/0
C / C++Code

If you read like writing a C/C++ program to print 1 to 100, it’s so much easy. Right?

I am sure everyone here can write a program to print a series of numbers using loop, recursion or goto statement.

But what about if you are asked to write a C/C++ program to print 1 to 100 without loop, recursion, or goto?

First question, Is it possible to do that?

Yes, it is.

So here you go.

C Program to Print 1 to 100 without Loop and Recursion:

The code is really simple but kind of tricky.

1. Using Class Constructor

The trick is to create 100 objects, and each object will print one value.

Follow the steps given below.

  • Write a class.
  • Create 100 objects of the class. (If you create the array of the object, you don’t need to use loop for each object.)
  • Write a constructor function in the class.
  • Print the value for each object.
  • Increment the value for each object by one.

Every call to constructor should write the different (incremental) value. You can do this by using a global variable. Each time it should call the constructor and increment the global variable by one. Then print the value.

Below is complete C++ code to program to print 1 to 100 without loop.

#include <iostream>
 using namespace std;
 
int i=0;
 
class S
{
 public:
    S()
    {
        i++;
        cout<<i<<endl;
    }
};
 
int main()
{
    S A[100];
    return 0;
}

You can print values in any given range.

You can initialize the global variable ‘i’ by any value which will be the first value in the range.
The number of objects of the class depicts total range.

There is also another solution by one of CSEstack subscriber.

2. Using Template

If you are in CSEstack subscriber list, you might have received an email where I asked this question as a part of quick contest.

Program to Print 1 to 100 without Loop

Not so surprised, I got a really good response from the many programmers. It’s not possible to post all the answers, so I have picked something unique code from all replies.

Here is C++ code by Jeviravikumar(GV).

#include <iostream>
using namespace std;

template<int N>
class PrintOneToN
{
public:
    static void print()
    {
        PrintOneToN<N-1>::print();  
        // Note that this is not recursion
        cout << N << endl;
    }
};

template<>
class PrintOneToN<1>
{
public:
    static void print()
    {
        cout << 1 << endl;
    }
};
int main()
{
    const int N = 100;
    PrintOneToN<N>::print();
    return 0;
}

Implementation is the same as an earlier program by creating N number of objects. Here, he has used the template as a generic data type.

This is the way you can print all the values from 1 to 100 without any recursion or loop. Many big tech companies ask this kind of tricky questions in their placement interviews.

Let’s take another quiz: Find Next Greatest Number with the Same Set of Digits.

This is the simple but tricky code program to print 1 to 100 without loop and recursion. I will keep sharing this kind of tricky coding questions. Subscribe to our channel and Stay tuned!

cpp
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

C Programming

  1. C- Introduction
  2. C- Compile & Execute Program
  3. C- Data Types
  4. C- if-else statement
  5. C- While, do-while, for loop
  6. C- Array
  7. C- Function (Types/Call)
  8. C- strlen() vs sizeof()
  9. C- Nested Switch Statement
  10. C- Recursion
  11. C- Dynamic Programming
  12. C- Storage Classes
  13. C- Creating Header File
  14. C- Null Pointer
  15. C- Stack and Queue
  16. C- Implement Stack using Array
  17. C- Implement Linked List in C
  18. C- File Handling
  19. C- Makefile Tutorial

Object Oriented Concepts in C++

  • C++: C vs OOPs Language
  • C++: Introduction to OOPs Concepts
  • C++: Inheritance

Sorting Algorithms

  • Different Types of Sorting Algo
  • Selection Sort
  • Bubble Sort
  • Quick Sort

Programming for Practice

  1. Online C/C++ Compiler

String Handling:

  1. Remove White Spaces from String
  2. Implement strstr Function in C
  3. Convert String to Int – atoi()
  4. Check if String is Palindrome
  5. Check if Two Strings are Anagram
  6. Split String in using strtok_r()
  7. Undefined reference to strrev

Array:

  1. Check if Array is Sorted

Bit Manipulation:

  1. Count Number of 1’s in Binary

Linked List:

  1. Reverse a Linked List Elements

Number System:

  1. Program to Find 2’s Complement
  2. Convert Decimal to Binary in C

Tricky Questions:

  1. Add Two Numbers without Operator
  2. Find Next Greater Number
  3. Swap values without temp variable
  4. Print 1 to 100 without Loop

Interview Coding Questions

  • 50+ Interview Coding Questions

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard