Program for Arrow Star Pattern
Given the value of n, print the pattern.
Examples :
Input : 5
Output :
*****
****
***
**
*
**
***
****
*****
Input : 7
Output :
*******
******
*****
****
***
**
*
**
***
****
*****
******
*******
Below is the implementation to print the above pattern :
C++
#include <bits/stdc++.h>
using namespace std;
int arrow( int n)
{
for ( int i = 1; i <= n; i++)
{
for ( int j = i; j < n; j++)
{
printf ( " " );
}
for ( int j = i; j <= n; j++)
{
cout << "*" ;
}
cout << endl;
}
for ( int i = 2; i <= n; i++)
{
for ( int j = 1; j < i; j++)
{
printf ( " " );
}
for ( int j = 1; j <= i; j++)
{
cout << "*" ;
}
cout << endl;
}
}
int main()
{
int n = 5;
arrow(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void arrow( int n)
{
for ( int i = 1 ; i <= n; i++)
{
for ( int j = i; j < n; j++)
{
System.out.print( " " );
}
for ( int j = i; j <= n; j++)
{
System.out.print( "*" );
}
System.out.println();
}
for ( int i = 2 ; i <= n; i++)
{
for ( int j = 1 ; j < i; j++)
{
System.out.print( " " );
}
for ( int j = 1 ; j <= i; j++)
{
System.out.print( "*" );
}
System.out.print( '\n' );
}
}
public static void main(String[] Argv) {
int n = 5 ;
arrow(n);
}
}
|
Python3
def arrow(n):
for i in range ( 1 , n + 1 ):
for j in range (i, n):
print ( " " , end = "")
for j in range (i, n + 1 ):
print ( "*" , end = "")
print ()
for i in range ( 2 , n + 1 ):
for j in range ( 1 , i):
print ( " " , end = "")
for j in range ( 1 , i + 1 ):
print ( "*" , end = "")
print ()
n = 5
arrow(n)
|
C#
using System;
class GFG {
static void arrow( int n)
{
for ( int i = 1; i <= n; i++)
{
for ( int j = i; j < n; j++)
{
Console.Write( " " );
}
for ( int j = i; j <= n; j++)
{
Console.Write( "*" );
}
Console.WriteLine();
}
for ( int i = 2; i <= n; i++)
{
for ( int j = 1; j < i; j++)
{
Console.Write( " " );
}
for ( int j = 1; j <= i; j++)
{
Console.Write( "*" );
}
Console.WriteLine();
}
}
public static void Main() {
int n = 5;
arrow(n);
}
}
|
PHP
<?php
function arrow( $n )
{
for ( $i = 1; $i <= $n ; $i ++)
{
for ( $j = $i ; $j < $n ; $j ++)
{
echo " " ;
}
for ( $j = $i ; $j <= $n ; $j ++)
{
echo "*" ;
}
echo "\n" ;
}
for ( $i = 2; $i <= $n ; $i ++)
{
for ( $j = 1; $j < $i ; $j ++)
{
echo " " ;
}
for ( $j = 1; $j <= $i ; $j ++)
{
echo "*" ;
}
echo "\n" ;
}
}
$n = 5;
arrow( $n );
?>
|
Javascript
<script>
function arrow(n)
{
for ( var i = 1; i <= n; i++)
{
for ( var j = i; j < n; j++)
{
document.write( " " );
}
for ( var j = i; j <= n; j++)
{
document.write( "*" );
}
document.write( "<br>" );
}
for ( var i = 2; i <= n; i++)
{
for ( var j = 1; j < i; j++) {
document.write( " " );
}
for ( var j = 1; j <= i; j++)
{
document.write( "*" );
}
document.write( "<br>" );
}
}
var n = 5;
arrow(n);
</script>
|
Output :
*****
****
***
**
*
**
***
****
*****
Time Complexity : O(n2) ,where n is the given input number.
Space Complexity : O(1) ,as we are not using any extra space.
Last Updated :
20 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...