> Write a program to print following pattern:
Solution:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A | |
AB | |
ABC | |
ABCD | |
ABCDE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
class test | |
{ | |
public static void main(String ar[]) | |
{ | |
Scanner scan = new Scanner(System.in); | |
System.out.println("Enter the limit:") | |
int limit = scan.nextInt(); | |
int x = limit-1; // to print the spaces. If lines=5, there should be 4 spaces in the first line. And then decrement x to get one space less in every new row. | |
char ch = 'A'; | |
for(int i=0;i<limit;i++) | |
{ | |
for(int k=0;k<x;k++) | |
{ | |
System.out.print(" "); | |
} | |
for(int j=0;j<=i;j++) | |
{ | |
System.out.print(ch); | |
ch++; | |
} | |
System.out.println(); | |
ch='A'; // re-init ch. Because you want the new line start with 'A' again. | |
x--; // DEcrement x to reduce the number of spaces in each row. | |
} | |
} | |
} |
Comments
Post a Comment