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 = 16
Output: 16, 11, 6, 1, -4, 1, 6, 11, 16
Input: n = 10
Output: 10, 5, 0, 5, 10
Print a pattern without using any loop (using recursion):
Follow the given steps to solve the problem:
- Create a recursive function with parameters as n and m and flag variable set as true
- Print m and if the flag is false and the value of m is equal to n then return from the function
- If the flag is true then check
- If m-5 is greater than zero then recur for m-5
- Else recur for m-5 and set the flag to false, as now we will be moving backward
- Else recur for m+5
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printPattern( int n, int m, bool flag)
{
cout << m << " " ;
if (flag == false && n == m)
return ;
if (flag) {
if (m - 5 > 0)
printPattern(n, m - 5, true );
else
printPattern(n, m - 5, false );
}
else
printPattern(n, m + 5, false );
}
void PrintPattern( int m)
{
if (m > 0) {
cout << m << '\n' ;
PrintPattern(m - 5);
}
cout << m << '\n' ;
}
int main()
{
int n = 16;
PrintPattern(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void printPattern( int n, int m, boolean flag)
{
System.out.print(m + " " );
if (flag == false && n == m)
return ;
if (flag) {
if (m - 5 > 0 )
printPattern(n, m - 5 , true );
else
printPattern(n, m - 5 , false );
}
else
printPattern(n, m + 5 , false );
}
public static void main(String[] args)
{
int n = 16 ;
printPattern(n, n, true );
}
}
|
Python3
def printPattern(n, m, flag):
print (m)
if flag = = False and n = = m:
return
if flag:
if m - 5 > 0 :
printPattern(n, m - 5 , True )
else :
printPattern(n, m - 5 , False )
else :
printPattern(n, m + 5 , False )
if __name__ = = "__main__" :
n = 16
printPattern(n, n, True )
|
C#
using System;
class GFG {
static void printPattern( int n, int m, bool flag)
{
Console.Write(m + " " );
if (flag == false && n == m)
return ;
if (flag) {
if (m - 5 > 0)
printPattern(n, m - 5, true );
else
printPattern(n, m - 5, false );
}
else
printPattern(n, m + 5, false );
}
public static void Main()
{
int n = 16;
printPattern(n, n, true );
}
}
|
PHP
<?php
function printPattern( $n , $m , $flag )
{
echo $m , " " ;
if ( $flag == false && $n == $m )
return ;
if ( $flag )
{
if ( $m - 5 > 0)
printPattern( $n , $m - 5, true);
else
printPattern( $n , $m - 5, false);
}
else
printPattern( $n , $m + 5, false);
}
$n = 16;
printPattern( $n , $n , true);
?>
|
Javascript
<script>
function printPattern(n, m, flag)
{
document.write(m + " " );
if (flag == false && n == m)
return ;
if (flag)
{
if (m - 5 > 0)
printPattern(n, m - 5, true );
else
printPattern(n, m - 5, false );
}
else
printPattern(n, m + 5, false );
}
let n = 16;
printPattern(n, n, true );
</script>
|
Output
16
11
6
1
-4
1
6
11
16
Time Complexity: O(N)
Auxiliary Space: O(N), stack space for the recursion
Print a pattern without using any loop and extra variable:
To solve the problem follow the below idea:
The above program works fine and prints the desired out but uses extra variables. We can use two print statements. The first one before the recursive call prints all decreasing sequences. The second one after the recursive call to print the increasing sequence
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void printPattern( int n)
{
if (n == 0 || n < 0) {
cout << n << " " ;
return ;
}
cout << n << " " ;
printPattern(n - 5);
cout << n << " " ;
}
int main()
{
int n = 16;
printPattern(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void printPattern( int n)
{
if (n == 0 || n < 0 ) {
System.out.print(n + " " );
return ;
}
System.out.print(n + " " );
printPattern(n - 5 );
System.out.print(n + " " );
}
public static void main(String[] args)
{
int n = 16 ;
printPattern(n);
}
}
|
Python3
def printPattern(n):
if (n = = 0 or n < 0 ):
print (n, end = ", " )
return
print (n, end = ", " )
printPattern(n - 5 )
print (n, end = ", " )
if __name__ = = "__main__" :
n = 16
printPattern(n)
|
C#
using System;
class GFG {
static void printPattern( int n)
{
if (n == 0 || n < 0) {
Console.Write(n + " " );
return ;
}
Console.Write(n + " " );
printPattern(n - 5);
Console.Write(n + " " );
}
public static void Main()
{
int n = 16;
printPattern(n);
}
}
|
PHP
<?php
function printPattern( $n )
{
if ( $n == 0 or $n < 0)
{
echo $n , " " ;
return ;
}
echo $n , " " ;
printPattern( $n -5);
echo $n , " " ;
}
$n = 16;
printPattern( $n );
?>
|
Javascript
<script>
function printPattern(n)
{
if (n == 0 || n < 0)
{
document.write(n + ", " );
return ;
}
document.write(n + ", " );
printPattern(n - 5);
document.write(n + ", " );
}
let n = 16;
printPattern(n);
</script>
|
Output
16 11 6 1 -4 1 6 11 16
Time Complexity: O(N)
Auxiliary Space: O(N), stack space for the recursion
Thanks to AKSHAY RATHORE for suggesting the above solution.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
20 Feb, 2023
Like Article
Save Article