Print Number series without using any loop
Last Updated :
26 Feb, 2023
Problem - Givens Two number N and K, our task is to subtract a number K from N until number(N) is greater than zero, once the N becomes negative or zero then we start adding K until that number become the original number(N).
Note : Not allow to use any loop.
Examples :
Input : N = 15 K = 5
Output : 15 10 5 0 1 5 10 15
Input : N = 20 K = 6
Output : 20 14 8 2 -4 2 8 14 20
Explanation - We can do it using recursion idea is that we call the function again and again until N is greater than zero (in every function call we subtract N by K). Once the number becomes negative or zero we start adding K in every function call until the number becomes the original number. Here we use a single function for both addition and subtraction but to switch between addition or subtraction function we used a Boolean flag.
C++
// C++ program to Print Number
// series without using loop
#include <iostream>
using namespace std;
// function print series
// using recursion
void PrintNumber(int N, int Original, int K, bool flag)
{
// print the number
cout << N << " ";
// change flag if number
// become negative
if (N <= 0)
flag = !flag;
// base condition for
// second_case (Adding K)
if (N == Original && !flag)
return;
// if flag is true
// we subtract value until
// number is greater than zero
if (flag == true) {
PrintNumber(N - K, Original, K, flag);
return;
}
// second case (Addition )
if (!flag) {
PrintNumber(N + K, Original, K, flag);
return;
}
}
// driver program
int main()
{
int N = 20, K = 6;
PrintNumber(N, N, K, true);
return 0;
}
Java
// Java program to Print Number
// series without using loop
import java.io.*;
import java.util.*;
class GFG
{
public static void PrintNumber(int N, int Original, int K, boolean flag)
{
// print the number
System.out.print(N + " ");
// change flag if number
// become negative
if (N <= 0)
flag = !flag;
// base condition for
// second_case (Adding K)
if (N == Original && !flag)
return;
// if flag is true
// we subtract value until
// number is greater than zero
if (flag == true)
{
PrintNumber(N - K, Original, K, flag);
return;
}
// second case (Addition )
if (!flag)
{
PrintNumber(N + K, Original, K, flag);
return;
}
}
public static void main (String[] args)
{
int N = 20, K = 6;
PrintNumber(N, N, K, true);
}
}
// This code is contributed by Mohit Gupta_OMG
Python3
# Python program to Print Number
# series without using loop
def PrintNumber(N, Original, K, flag):
#print the number
print(N, end = " ")
# change flag if number
# become negative
if (N <= 0):
if(flag==0):
flag = 1
else:
flag = 0
# base condition for
# second_case (Adding K)
if (N == Original and (not(flag))):
return
# if flag is true
# we subtract value until
# number is greater than zero
if (flag == True):
PrintNumber(N - K, Original, K, flag)
return
# second case (Addition )
if (not(flag)):
PrintNumber(N + K, Original, K, flag);
return
N = 20
K = 6
PrintNumber(N, N, K, True)
# This code is contributed by Mohit Gupta_OMG
C#
// C# program to Print Number
// series without using loop
using System;
public class GFG {
// function print series
// using recursion
static void PrintNumber(int N,
int Original, int K, bool flag)
{
// print the number
Console.Write(N + " ");
// change flag if number
// become negative
if (N <= 0)
flag = !flag;
// base condition for
// second_case (Adding K)
if (N == Original && !flag)
return;
// if flag is true
// we subtract value until
// number is greater than zero
if (flag == true)
{
PrintNumber(N - K, Original, K,
flag);
return;
}
// second case (Addition )
if (!flag)
{
PrintNumber(N + K, Original, K,
flag);
return;
}
}
// driver program
static public void Main ()
{
int N = 20, K = 6;
PrintNumber(N, N, K, true);
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP program to Print Number
// series without using loop
// function print series
// using recursion
function PrintNumber($N, $Original,
$K, $flag)
{
// print the number
echo($N . " ");
// change flag if number
// become negative
if ($N <= 0)
$flag = !$flag;
// base condition for
// second_case (Adding K)
if ($N == $Original && !$flag)
return;
// if flag is true
// we subtract value until
// number is greater than zero
if ($flag == true) {
PrintNumber($N - $K, $Original,
$K, $flag);
return;
}
// second case (Addition )
if (!$flag) {
PrintNumber($N + $K, $Original,
$K, $flag);
return;
}
}
// Driver Code
$N = 20; $K = 6;
PrintNumber($N, $N, $K, true);
// This code is contributed by Ajit.
?>
JavaScript
<script>
// Javascript program to Print Number
// series without using loop
// function print series
// using recursion
function PrintNumber(N, Original,
K, flag)
{
// print the number
document.write(N + " ");
// change flag if number
// become negative
if (N <= 0)
flag = !flag;
// base condition for
// second_case (Adding K)
if (N == Original && !flag)
return;
// if flag is true
// we subtract value until
// number is greater than zero
if (flag == true) {
PrintNumber(N - K, Original,
K, flag);
return;
}
// second case (Addition )
if (!flag) {
PrintNumber(N + K, Original,
K, flag);
return;
}
}
// Driver Code
let N = 20, K = 6;
PrintNumber(N, N, K, true);
// This code is contributed by _saurabh_jaiswal
</script>
Output20 14 8 2 -4 2 8 14 20
Time complexity: O(N/K)
Auxiliary space: O(N/K)
The extra space is used in recursion call stack.
Similar Reads
Find the nth term of the given series Given the first two terms of the series as 1 and 6 and all the elements of the series are 2 less than the mean of the number preceding and succeeding it. The task is to print the nth term of the series. First few terms of the series are: 1, 6, 15, 28, 45, 66, 91, ... Examples: Input: N = 3 Output: 1
3 min read
Find Nth term of the series 1, 1, 2, 6, 24... Given a number N. The task is to write a program to find the Nth term in the below series: 1, 1, 2, 6, 24... Examples: Input: 3 Output: 2 For N = 3 Nth term = (N-1)! = 2 Input: 5 Output: 24 Nth term of the series is given by below formula: Nth term = ( N-1)! Below is the required implementation: C++
3 min read
Find Nth term of the series 1, 8, 54, 384... Given a number N. The task is to write a program to find the Nth term in the below series: 1, 8, 54, 384... Examples: Input : 3 Output : 54 For N = 3 Nth term = ( 3*3) * 3! = 54 Input : 2 Output : 8 On observing carefully, the Nth term in the above series can be generalized as: Nth term = ( N*N ) *
4 min read
Find Nth term of the series 5, 13, 25, 41, 61... Given a number N. The task is to write a program to find the Nth term in the below series: 5, 13, 25, 41, 61... Examples: Input : 3 Output : 25 For N = 3 Nth term = 3*3 + (3+1)*(3+1) = 25 Input : 5 Output : 61 On observing carefully, the Nth term of the given series can be generalised as: Nth term =
3 min read
Find Nth term of the series 0, 6, 0, 12, 0, 90... Given a number N. The task is to write a program to find the Nth term in the below series: 0, 6, 0, 12, 0, 90... Examples: Input : N = 4 Output : 12 For N = 4 Nth term = abs( 4 * ((4-1) * (4-3) * (4-5)) ); = 12 Input : N = 6 Output : 90 We can generalize the Nth term of the above series as: Nth term
3 min read