Given the number of lines n, print the alphabet A pattern using stars.
Examples :
Input : Number of lines : 5
Output :
*
* *
***
* *
* *
Input : Number of lines : 10
Output :
****
* *
* *
* *
* *
******
* *
* *
* *
* *
C++
#include <iostream>
using namespace std;
void display( int n)
{
for ( int i = 0; i < n; i++) {
for ( int j = 0; j <= n / 2; j++) {
if ((j == 0 || j == n / 2) && i != 0 ||
i == 0 && j != 0 && j != n / 2 ||
i == n / 2)
cout << "*" ;
else
cout << " " ;
}
cout << '\n' ;
}
}
int main()
{
display(7);
return 0;
}
|
Java
import java.util.Scanner;
class PatternA {
void display( int n)
{
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j <= n / 2 ; j++) {
if ((j == 0 || j == n / 2 ) && i != 0 ||
i == 0 && j != 0 && j != n / 2 ||
i == n / 2 )
System.out.print( "*" );
else
System.out.print( " " );
}
System.out.println();
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
PatternA a = new PatternA();
a.display( 7 );
}
}
|
Python3
def display(n):
for i in range (n):
for j in range ((n / / 2 ) + 1 ):
if ((j = = 0 or j = = n / / 2 ) and i ! = 0 or
i = = 0 and j ! = 0 and j ! = n / / 2 or
i = = n / / 2 ):
print ( "*" , end = "")
else :
print ( " " , end = "")
print ()
display( 7 )
|
C#
using System;
class PatternA {
void display( int n)
{
for ( int i = 0; i < n; i++) {
for ( int j = 0; j <= n / 2; j++) {
if ((j == 0 || j == n / 2) && i != 0 ||
i == 0 && j != 0 && j != n / 2 ||
i == n / 2)
Console.Write( "*" );
else
Console.Write( " " );
}
Console.WriteLine();
}
}
public static void Main()
{
PatternA a = new PatternA();
a.display(7);
}
}
|
PHP
<?php
function display( $n )
{
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j <= floor ( $n / 2); $j ++)
{
if (( $j == 0 || $j == floor ( $n / 2)) &&
$i != 0 || $i == 0 && $j != 0 &&
$j != floor ( $n / 2) ||
$i == floor ( $n / 2))
echo "*" ;
else
echo " " ;
}
echo "\n" ;
}
}
$n =7;
display( $n );
?>
|
Javascript
<script>
function display(n)
{
for ( var i = 0; i < n; i++)
{
for ( var j = 0; j <= Math.floor(n / 2); j++)
{
if (
((j == 0 || j == Math.floor(n / 2)) && i != 0) ||
(i == 0 && j != 0 && j != Math.floor(n / 2)) ||
i == Math.floor(n / 2)
) {
document.write( "*" );
} else document.write( " " );
}
document.write( "<br>" );
}
}
display(7);
</script>
|
Output :
**
* *
* *
****
* *
* *
* *
Time Complexity: O(n*n)
Auxiliary Space: O(1)
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 :
17 Feb, 2023
Like Article
Save Article