- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C program to print number series without using any loop
In this problem, we are given two numbers N and K. Our task is to create a program that will print number series without using any loop.
The series that is to be printed will start from n and will be subtracted by k till it becomes zero or negative. After that, we will start to add k to it till it becomes n again. If this process we cannot use any type of loop.
Let’s take an example to understand the problem,
Input
n = 12 , k = 3
Output
12 9 6 3 0 3 6 9 12
To solve this problem without using a loop we will use recursion. We will create a recursive function that will call itself again and keep a check on the value of the number to ensure which operation out of addition or subtraction is to be one on the number.
The function will use a flag that will help us to keep track of whether the value is to be subtracted or added.
C program to print number series without using any loop
// C program to print number series without using any loop
Example
#include <iostream> using namespace std; void PrintSeriesRec(int current, int N, int K, bool flag){ cout<<current<<"\t"; if (current <= 0) flag = !flag; if (current == N && !flag) return; if (flag == true) PrintSeriesRec(current - K, N, K, flag); else if (!flag) PrintSeriesRec(current + K, N, K, flag); } int main(){ int N = 12, K = 4; cout<<"The series is :
"; PrintSeriesRec(N, N, K, true); return 0; }
Output
The series is −
12 8 4 0 4 8 12
- Related Articles
- Java Program to print Number series without using any loop
- Print Number series without using any loop in Python Program
- Python Program for Print Number series without using any loop
- Print a pattern without using any loop in C++
- Write a C program to print ‘ABCD’ repeatedly without using loop, recursion and any control structure
- Print m multiplies of n without using any loop in Python.
- Java program to print the fibonacci series of a given number using while loop
- Program to print numbers from 1 to 100 without using loop
- Print first m multiples of n without using any loop in Python
- Print a number 100 times without using loop, recursion and macro expansion in C
- C program to print a string without any quote in the program
- How to print a name multiple times without loop statement using C language?
- C program to print multiplication table by using for Loop
- Print “Hello World” without using any header file in C
- C program to print characters without using format specifiers
