递归打印给定的图案

给定一个正整数 N .使用递归方法打印倒三角形图案(如下例所述)。

null

例如:

Input : n = 5Output : * * * * ** * * ** * ** **Input : n = 7Output :* * * * * * ** * * * * ** * * * * * * * ** * ** **

方法1(使用两个递归函数): 一个递归函数用于获取行号,另一个递归函数用于打印特定行的星号。

算法:

printPatternRowRecur(n)    if n < 1        return            print "* "    printPatternRowRecur(n-1)printPatternRecur(n)    if n < 1        return        printPatternRowRecur(n)    print ""    printPatternRecur(n-1)

C++

// C++ implementation to print the given
// pattern recursively
#include <bits/stdc++.h>
using namespace std;
// function to print the 'n-th' row of the
// pattern recursively
void printPatternRowRecur( int n)
{
// base condition
if (n < 1)
return ;
// print the remaining stars of the n-th row
// recursively
cout << "* " ;
printPatternRowRecur(n-1);
}
void printPatternRecur( int n)
{
// base condition
if (n < 1)
return ;
// print the stars of the n-th row
printPatternRowRecur(n);
// move to next line
cout << endl;
// print stars of the remaining rows recursively
printPatternRecur(n-1);
}
// Driver program to test above
int main()
{
int n = 5;
printPatternRecur(n);
return 0;
}


JAVA

// java implementation to print the given
// pattern recursively
import java.io.*;
class GFG
{
// function to print the 'n-th' row
// of the pattern recursively
static void printPatternRowRecur( int n)
{
// base condition
if (n < 1 )
return ;
// print the remaining stars
// of the n-th row recursively
System.out.print( "* " );
printPatternRowRecur(n - 1 );
}
static void printPatternRecur( int n)
{
// base condition
if (n < 1 )
return ;
// print the stars of the n-th row
printPatternRowRecur(n);
// move to next line
System.out.println ();
// print stars of the
// remaining rows recursively
printPatternRecur(n - 1 );
}
// Driver program to test above
public static void main (String[] args)
{
int n = 5 ;
printPatternRecur(n);
}
}
//This code is contributed by vt_m


Python3

# Python 3 implementation
# to print the given
# pattern recursively
# function to print the
# 'n-th' row of the
# pattern recursively
def printPatternRowRecur(n):
# base condition
if (n < 1 ):
return
# print the remaining
# stars of the n-th row
# recursively
print ( "*" , end = " " )
printPatternRowRecur(n - 1 )
def printPatternRecur(n):
# base condition
if (n < 1 ):
return
# print the stars of
# the n-th row
printPatternRowRecur(n)
# move to next line
print ("")
# print stars of the
# remaining rows recursively
printPatternRecur(n - 1 )
# Driver Code
n = 5
printPatternRecur(n)
# This code is contributed
# by Smitha


C#

// C# implementation to print the given
// pattern recursively
using System;
class GFG
{
// function to print the 'n-th' row
// of the pattern recursively
static void printPatternRowRecur( int n)
{
// base condition
if (n < 1)
return ;
// print the remaining stars
// of the n-th row recursively
Console.Write( "* " );
printPatternRowRecur(n - 1);
}
static void printPatternRecur( int n)
{
// base condition
if (n < 1)
return ;
// print the stars of the n-th row
printPatternRowRecur(n);
// move to next line
Console.WriteLine();
// print stars of the
// remaining rows recursively
printPatternRecur(n - 1);
}
// Driver program to test above
public static void Main()
{
int n = 5;
printPatternRecur(n);
}
}
//This code is contributed by vt_m


PHP

<?php
// php implementation to print the given
// pattern recursively
// function to print the 'n-th' row
// of the pattern recursively
function printPatternRowRecur( $n )
{
// base condition
if ( $n < 1)
return ;
// print the remaining stars of
// the n-th row recursively
echo "* " ;
printPatternRowRecur( $n -1);
}
function printPatternRecur( $n )
{
// base condition
if ( $n < 1)
return ;
// print the stars of the n-th row
printPatternRowRecur( $n );
// move to next line
echo "" ;
// print stars of the remaining
// rows recursively
printPatternRecur( $n -1);
}
// Driver code
$n = 5;
printPatternRecur( $n );
// This code is contributed by mits
?>


Javascript

<script>
// JavaScript implementation to print the given
// pattern recursively
// function to print the 'n-th' row
// of the pattern recursively
function printPatternRowRecur(n)
{
// base condition
if (n < 1)
return ;
// print the remaining stars
// of the n-th row recursively
document.write( "* " );
printPatternRowRecur(n - 1);
}
function printPatternRecur(n)
{
// base condition
if (n < 1)
return ;
// print the stars of the n-th row
printPatternRowRecur(n);
// move to next line
document.write( "<br>" );
// print stars of the
// remaining rows recursively
printPatternRecur(n - 1);
}
// Driver program to test above
var n = 5;
printPatternRecur(n);
// This code is contributed by Amit Katiyar
</script>


输出:

* * * * ** * * ** * ** **

方法2(使用单个递归函数): 这种方法使用一个递归函数来打印整个图案。

算法:

printPatternRecur(n, i)    if n < 1        return        if i <= n        print "* "        printPatternRecur(n, i+1)            else        print ""        printPatternRecur(n-1, 1)

C++

// C++ implementation to print the given pattern recursively
#include <bits/stdc++.h>
using namespace std;
// function to print the given pattern recursively
void printPatternRecur( int n, int i)
{
// base condition
if (n < 1)
return ;
// to print the stars of a particular row
if (i <= n)
{
cout << "* " ;
// recursively print rest of the stars
// of the row
printPatternRecur(n, i + 1);
}
else
{
// change line
cout << endl;
// print stars of the remaining rows recursively
printPatternRecur(n-1, 1);
}
}
// Driver program to test above
int main()
{
int n = 5;
printPatternRecur(n, 1);
return 0;
}


JAVA

// java implementation to
// print the given pattern recursively
import java.io.*;
class GFG {
// function to print the
// given pattern recursively
static void printPatternRecur( int n, int i)
{
// base condition
if (n < 1 )
return ;
// to print the stars of
// a particular row
if (i <= n)
{
System.out.print ( "* " );
// recursively print rest
// of the stars of the row
printPatternRecur(n, i + 1 );
}
else
{
// change line
System.out.println( );
// print stars of the
// remaining rows recursively
printPatternRecur(n - 1 , 1 );
}
}
// Driver program
public static void main (String[] args)
{
int n = 5 ;
printPatternRecur(n, 1 );
}
}
// This code is contributed by vt_m


Python3

# Python3 implementation to print the
# given pattern recursively
# function to print the given pattern
# recursively
def printPatternRecur(n, i):
# base condition
if (n < 1 ):
return
# to print the stars of a
# particular row
if (i < = n):
print ( "* " , end = "")
# recursively print rest of
# the stars of the row
printPatternRecur(n, i + 1 )
else :
# change line
print ("")
# print stars of the remaining
# rows recursively
printPatternRecur(n - 1 , 1 )
# Driver program to test above
n = 5
printPatternRecur(n, 1 )
# This code is contributed by Smitha


C#

// C# implementation to
// print the given pattern recursively
using System;
class GFG {
// function to print the
// given pattern recursively
static void printPatternRecur( int n, int i)
{
// base condition
if (n < 1)
return ;
// to print the stars of
// a particular row
if (i <= n)
{
Console.Write ( "* " );
// recursively print rest
// of the stars of the row
printPatternRecur(n, i + 1);
}
else
{
// change line
Console.WriteLine( );
// print stars of the
// remaining rows recursively
printPatternRecur(n - 1, 1);
}
}
// Driver program
public static void Main ()
{
int n = 5;
printPatternRecur(n, 1);
}
}
// This code is contributed by vt_m


PHP

<?php
// php implementation to print
// the given pattern recursively
// function to print the given
// pattern recursively
function printPatternRecur( $n , $i )
{
// base condition
if ( $n < 1)
return ;
// to print the stars of
// a particular row
if ( $i <= $n )
{
echo "* " ;
// recursively print rest of
// the stars  of the row
printPatternRecur( $n , $i + 1);
}
else
{
// change line
echo "" ;
// print stars of the remaining
// rows recursively
printPatternRecur( $n - 1, 1);
}
}
// Driver code
$n = 5;
printPatternRecur( $n , 1);
// This code is contributed by mits
?>


Javascript

<script>
// JavaScript implementation to print
// the given pattern recursively
// Function to print the given
// pattern recursively
function printPatternRecur(n, i)
{
// Base condition
if (n < 1)
return ;
// To print the stars of
// a particular row
if (i <= n)
{
document.write( "*" + "  " );
// Recursively print rest of the stars
// of the row
printPatternRecur(n, i + 1);
}
else
{
// Change line
document.write( "<br>" );
// Print stars of the remaining
// rows recursively
printPatternRecur(n - 1, 1);
}
}
// Driver code
var n = 5;
printPatternRecur(n, 1);
// This code is contributed by rdtank
</script>


输出:

* * * * ** * * ** * ** **

本文由 阿尤什·焦哈里 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享