One uppercase English letter C is given. Print a letter pyramid of k + 1 lines from A to C (k is how many letters C is after A; 0 for A). Line i from the top (counting from 0) is k - i spaces, then the letters from A up to the letter i places after A, then back down from the letter just before it to A. For example, for C = D the last line is "ABCDCBA". Do not put spaces at the end of a line.
Input
C is one uppercase letter from A to Z
Output
a pyramid of k + 1 lines
Example 1 · stdin
D
stdout
A
ABA
ABCBA
ABCDCBA
Example 2 · stdin
A
stdout
A
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
Turning a letter into a number, adding, and turning it back differs by language: Python chr(ord("A") + j), JS/TS String.fromCharCode(65 + j). In JS, "A" + 1 is string concatenation, not arithmetic, so it gives "A1". In C, C++, Java and C#, 'A' + j is an int. C's putchar prints an int as a character, but C++ cout << 'A' + j and Java StringBuilder.append('A' + j) print a number like 65. Convert back with (char) or static_cast<char>. Repeating a character differs too: Python " " * n, JS/TS/Java (11+) " ".repeat(n), C# new string(' ', n), C++ std::string(n, ' '). C has no repeat function, so use a loop or print a width of spaces with printf("%*s", n, ""). Craykon gets the number with Str.Code("A"), turns it back with Str.FromCode(a + j), and makes the spaces with " ".Repeat(k - i).
Pythonmain.py · 5 lines
k = ord(input().strip()) - ord("A")for i in range(k + 1): up = "".join(chr(ord("A") + j) for j in range(i + 1)) # 올라간 글자 + 마지막 글자를 뺀 거꾸로 print(" " * (k - i) + up + up[-2::-1])
JavaScriptmain.js · 9 lines
const k = require("fs").readFileSync(0, "utf8").trim().charCodeAt(0) - 65;const lines = [];for (let i = 0; i <= k; i++) { let up = ""; for (let j = 0; j <= i; j++) up += String.fromCharCode(65 + j); const down = up.slice(0, -1).split("").reverse().join(""); lines.push(" ".repeat(k - i) + up + down);}console.log(lines.join("\n"));
TypeScriptmain.ts · 10 lines
import { readFileSync } from "node:fs";const k: number = readFileSync(0, "utf8").trim().charCodeAt(0) - 65;const lines: string[] = [];for (let i = 0; i <= k; i++) { let up = ""; for (let j = 0; j <= i; j++) up += String.fromCharCode(65 + j); const down: string = up.slice(0, -1).split("").reverse().join(""); lines.push(" ".repeat(k - i) + up + down);}console.log(lines.join("\n"));
JavaMain.java · 16 lines
import java.util.Scanner;public class Main { public static void main(String[] args) { int k = new Scanner(System.in).next().charAt(0) - 'A'; StringBuilder out = new StringBuilder(); for (int i = 0; i <= k; i++) { out.append(" ".repeat(k - i)); // 'A' + j 는 int라서 (char)로 되돌려야 글자로 붙는다 for (int j = 0; j <= i; j++) out.append((char) ('A' + j)); for (int j = i - 1; j >= 0; j--) out.append((char) ('A' + j)); out.append('\n'); } System.out.print(out); }}
C#Main.cs · 20 lines
using System;using System.Text;class Program{ static void Main() { int k = Console.ReadLine().Trim()[0] - 'A'; var output = new StringBuilder(); for (int i = 0; i <= k; i++) { output.Append(new string(' ', k - i)); // 'A' + j 는 int라서 (char)로 되돌려야 글자로 붙는다 for (int j = 0; j <= i; j++) output.Append((char)('A' + j)); for (int j = i - 1; j >= 0; j--) output.Append((char)('A' + j)); output.Append('\n'); } Console.Write(output); }}
C++main.cpp · 15 lines
#include <iostream>#include <string>int main() { char c; std::cin >> c; int k = c - 'A'; for (int i = 0; i <= k; i++) { std::string line(k - i, ' '); // 'A' + j 는 int라서 char로 되돌려 붙인다 for (int j = 0; j <= i; j++) line += static_cast<char>('A' + j); for (int j = i - 1; j >= 0; j--) line += static_cast<char>('A' + j); std::cout << line << '\n'; }}
Cmain.c · 15 lines
#include <stdio.h>int main(void) { char c; scanf(" %c", &c); int k = c - 'A'; for (int i = 0; i <= k; i++) { // C에는 되풀이 함수가 없다: %*s 에 빈 문자열을 주면 그 너비만큼 빈칸이 찍힌다 printf("%*s", k - i, ""); for (int j = 0; j <= i; j++) putchar('A' + j); for (int j = i - 1; j >= 0; j--) putchar('A' + j); putchar('\n'); } return 0;}
Craykonmain.cray · 15 lines
a = Str.Code("A");k = Str.Code(Console.ReadLine().Trim()) - a;for (i = 0; i <= k; i++){ line = " ".Repeat(k - i); for (j = 0; j <= i; j++) { line += Str.FromCode(a + j); } for (j = i - 1; j >= 0; j--) { line += Str.FromCode(a + j); } Console.WriteLine(line);}