使用星星打印字母“A”的程序
给定行数 n,用星号打印字母 A 模式。
例子 :
Input : Number of lines : 5
Output :
*
* *
***
* *
* *
Input : Number of lines : 10
Output :
****
* *
* *
* *
* *
******
* *
* *
* *
* *
C++
// CPP program to print alphabet A pattern
#include
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
Javascript
输出 :
**
* *
* *
****
* *
* *
* *