Character triangle program in Java
import java.util.Scanner;
public class CharacterTriangle
{
public static void main(String[] args) {
char ch;
Scanner sc = new Scanner(System.in);
System.out.print("Enter pyramid character : ");
ch = sc.next().charAt(0);
if (ch >= 'a' && ch <= 'z') {
ch = (char)(ch - 32);
}
for (char r = 'A'; r <= ch; r++) {
for (int sp = r; sp > 'A'; sp--) {
System.out.print(" ");
}
for (char c = r; c <= ch; c++) {
System.out.print(c);
}
for (char c = (char)(ch - 1); r <= c; c--) {
System.out.print(c);
}
System.out.println();
}
}
}
Output:
Enter pyramid character : e
ABCDEDCBA
BCDEDCB
CDEDC
DED
E
BUILD SUCCESSFUL (total time: 2 seconds)
import java.util.Scanner;
public class CharacterTriangle
{
public static void main(String[] args) {
char ch;
Scanner sc = new Scanner(System.in);
System.out.print("Enter pyramid character : ");
ch = sc.next().charAt(0);
if (ch >= 'a' && ch <= 'z') {
ch = (char)(ch - 32);
}
for (char r = 'A'; r <= ch; r++) {
for (int sp = r; sp > 'A'; sp--) {
System.out.print(" ");
}
for (char c = r; c <= ch; c++) {
System.out.print(c);
}
for (char c = (char)(ch - 1); r <= c; c--) {
System.out.print(c);
}
System.out.println();
}
}
}
Output:
Enter pyramid character : e
ABCDEDCBA
BCDEDCB
CDEDC
DED
E
BUILD SUCCESSFUL (total time: 2 seconds)
Note: Hello friends, If you have any better solution for this programs then please comment below, I will add your comment as a solution for this programs.
Thanks for visiting my blog.
|
No comments:
Post a Comment