Dado el número de líneas n, imprime el patrón del alfabeto A usando estrellas.
Ejemplos:
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>
Producción :
** * * * * **** * * * * * *
Publicación traducida automáticamente
Artículo escrito por Bishal Kumar Dubey y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA