Using Java Write a program that asks the user for a size n a
Using Java Write a program that asks the user for a size (n) and a character, and then outputs a pyramid pattern of that size using the character. Hint: The total number of characters you need to print in the last row will always be 2n - 1. Example Output: Enter the size of the pyramid: 4 Enter the character to use: a a aaa aaaaa aaaaaaa
Solution
Pyramid.java
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
int size;
char ch;
// Scanner Object is used to read the inputs entered by the user
Scanner sc = new Scanner(System.in);
//Getting the size of the Pyramid Entered by the user
System.out.print(\"Enter Size of the pyrimid :\");
size = sc.nextInt();
//Getting the Character entered by the user
System.out.print(\"Enter the Character to use :\");
ch = sc.next(\".\").charAt(0);
//This will Prints the required Pyramid on the console
for (int a = 0; a < 2 * size - 1; a++) {
//Based on this condition it will prints character odd number of times.
if (a % 2 == 0) {
for (int b = 0; b < (2 * size - 1) - a; b++) {
System.out.print(\" \");
}
for (int c = 0; c <= a; c++) {
System.out.print(ch+\" \");
}
System.out.println();
}
}
}
}
__________________________________________
Output:
Enter Size of the pyrimid :4
Enter the Chiriter to use :a
a
a a a
a a a a a
a a a a a a a
___________________________Thank You
