问题–给定两个数N和K,我们的任务是从N中减去数K,直到number(N)大于零,一旦N变为负数或零,我们就开始加K直到该数成为原始数(N )。
注意:不允许使用任何循环。
例子 :
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
解释–我们可以使用递归的想法来做到这一点,那就是我们一次又一次地调用该函数,直到N大于零(在每个函数调用中,我们将N减去K)。一旦数字变为负数或零,我们就开始在每个函数调用中添加K,直到数字变为原始数字为止。这里我们使用一个单一的函数加法和减法,而是加法或减法函数之间,我们使用了布尔标志开关。
C++
// C++ program to Print Number
// series without using loop
#include
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 then 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 then 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 then 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 then 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
输出 :
20 14 8 2 -4 2 8 14 20