Open In App
Related Articles

Program to print alphabet “A” using stars

Improve Article
Improve
Save Article
Save
Like Article
Like

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++




// CPP program to print alphabet A pattern
#include <iostream>
using namespace std;
  
// Function to display alphabet pattern
void display(int n)
{
    // Outer for loop for number of lines
    for (int i = 0; i < n; i++) {
  
        // Inner for loop for logic execution
        for (int j = 0; j <= n / 2; j++) {
  
            // prints two column lines
            if ((j == 0 || j == n / 2) && i != 0 ||
  
                // print first line of alphabet
                i == 0 && j != 0 && j != n / 2 ||
  
                // prints middle line
                i == n / 2)
                cout << "*";
            else
                cout << " ";
        }
  
        cout << '\n';
    }
}
// Driver Function
int main()
{
    display(7);
    return 0;
}


Java




// Java program to print alphabet A pattern
import java.util.Scanner;
class PatternA {
    void display(int n)
    {
        // Outer for loop for number of lines
        for (int i = 0; i < n; i++) {
  
            // Inner for loop for logic execution
            for (int j = 0; j <= n / 2; j++) {
  
                // prints two column lines
                if ((j == 0 || j == n / 2) && i != 0 ||
  
                    // print first line of alphabet
                    i == 0 && j != 0 && j != n / 2 ||
  
                    // prints middle line
                    i == n / 2)
  
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
  
            System.out.println();
        }
    }
  
    // Driver Function
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        PatternA a = new PatternA();
        a.display(7);
    }
}


Python3




# Python3 program to print alphabet A pattern
  
# Function to display alphabet pattern
def display(n):
  
    # Outer for loop for number of lines
    for i in range(n):
  
        # Inner for loop for logic execution
        for j in range((n // 2) + 1):
  
            # prints two column lines
            if ((j == 0 or j == n // 2) and i != 0 or
  
                # print first line of alphabet
                i == 0 and j != 0 and j != n // 2 or
  
                # prints middle line
                i == n // 2):
                print("*", end = "")
            else:
                print(" ", end = "")
          
        print()
      
  
# Driver Function
display(7)
  
  
# This code is contributed by Anant Agarwal. 


C#




// C# program to print alphabet A pattern
using System;
class PatternA {
    void display(int n)
    {
        // Outer for loop for number of lines
        for (int i = 0; i < n; i++) {
  
            // Inner for loop for logic execution
            for (int j = 0; j <= n / 2; j++) {
  
                // prints two column lines
                if ((j == 0 || j == n / 2) && i != 0 ||
  
                    // print first line of alphabet
                    i == 0 && j != 0 && j != n / 2 ||
  
                    // prints middle line
                    i == n / 2)
  
                Console.Write("*");
                else
                Console.Write(" ");
            }
  
            Console.WriteLine();
        }
    }
  
    // Driver Function
    public static void Main()
    {
        PatternA a = new PatternA();
        a.display(7);
    }
}
/*This code is contributed by vt_m.*/


PHP




<?php
// php program to print
// alphabet A pattern
  
// Function to display
// alphabet pattern
function display($n)
{
      
    // Outer for loop for 
    // number of lines
    for ($i = 0; $i < $n; $i++)
    {
  
        // Inner for loop for 
        // logic execution
        for ($j = 0; $j <= floor($n / 2); $j++)
        {
  
            // prints two column lines
            // print first line of alphabet
            // prints middle line            
            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";
    }
}
// Driver Function
$n=7;
display($n);
  
// This code is contributed by mits
?>


Javascript




<script>
  
  // JavaScript program to print alphabet A pattern
  
  // Function to display alphabet pattern
  function display(n) 
  {
    // Outer for loop for number of lines
    for (var i = 0; i < n; i++) 
    {
      // Inner for loop for logic execution
      for (var j = 0; j <= Math.floor(n / 2); j++)
      {
        // prints two column lines
        if (
          ((j == 0 || j == Math.floor(n / 2)) && i != 0) ||
          // print first line of alphabet
          (i == 0 && j != 0 && j != Math.floor(n / 2)) ||
          // prints middle line
          i == Math.floor(n / 2)
        ) {
          document.write("*");
        } else document.write("  ");
      }
      document.write("<br>");
    }
  }
  // Driver Function
  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
Previous
Next
Similar Reads
Complete Tutorials