@
quicky008
Code:
import java.util.Scanner;
public class PatternNumPyramid {
public static void main(String args[]) {
System.out.println("Enter the number of rows you want to print");
Scanner input = new Scanner(System.in);
int numOfRows = input.nextInt();
int i, j, k; [I]//to be used in for{} loops[/I]
boolean flag; [I] //flag is used to ensure that loop k(for space) runs only once after coming to next line[/I]
for(i=0 ; i<=numOfRows; i++){ [I]//i is to go to next line[/I]
flag=true;
for(j=i; j>=0; j--){ [I]//j is to print in decreasing order[/I]
if(flag){
for(k=numOfRows-j; k>0; k--){ [I] //k is to print spaces[/I]
System.out.print(" ");
}flag=false;
}
System.out.print(j);
if(i>0){
if(j==0){
for(int x=1; x<=i; x++){ [I] //x is to print in increasing order[/I]
System.out.print(x);
}
}
}
}
System.out.println("");
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1.
numOfRows :
(a) As you see, the base of the pyramid starts with digit 5, and total number of rows you want to print are 5+1(=6). So I decided to take that number as common and it also helps to count number of spaces in each row(explained below)
(b) In the condition of the first loop, i<=5(
number of rows), is used to print last row which starts from the digit 5. If you use i<5(
number of rows) then it will just print 5 rows where 5 row starts from digit 4. You will miss the last row starting from 5.
(c) The above source code is in Java, so I used Scanner object
input to take the common value of number of rows, digits at last row and space counts. Scanner is a class from util Library of Java, available to take input from the console. System.in parameter is input stream connected to Keyboard. I used it so that You can print the pyramid with any numerical value.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2. Spaces :
(a)Then comes the part about spaces. How to decide how many spaces should come?
If you see carefully, you will see that in the first row, there are five spaces and then 0 is printed. In next row, four spaces and then comes the 101. The spaces keep decreasing as we go to next row. So I decided to start the number of spaces should be equal to number of rows we want to print. ANd then number of rows should be keep on decreasing as we move to next row.
(b)How to decrease number of spaces in next row you ask? Remember, the spaces has to decrease by one count at each row. I used the logic as,
number of spaces at current row(
k) = required number of rows(
numOfRows) - value of current row(
i) or value of
j
(i) number of current row-is obvious one, for example
number of spaces at current row = 5 - 0 (value of current row or i) = 5. then we come to next row,
number of spaces at current row = 5 - 1 (value of current row or i) = 4, next row
number of spaces at current row = 5 - 2 (value of current row or i) = 3, and so on
(ii) how can we use j at the same position you ask? Notice the first digit in each row, it is count in increasing order. We can use same logic as above to find the number of spaces required at the current row.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3. Digits :
(a) Forget everything and concentrate on just one row. I am using variable
j to print the digits in increasing order and
x for printing in increasing order. For example, in second last row, 4 3 2 1 0 1 2 3 4 . Here, I used
j to print 4 3 2 1 0 and then I checked the value of
j. If the value of
j is 0, then I need to print the digits in increasing order, so I declared a new variable
x to and print the digits till the value of
j, i.e. 4.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
I made some comments in few lines, to explain why did I take certain variables and their values. Try to change them and experiment a bit to understand the flow of the program. For example you can remove that flag variable and the check the output to see why I needed that variable. Use i to calculate the number of spaces required and check output and so on.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output -
How I approached for solution?
a. I just wrote a code to print values in decreasing order like 0 10 210 3210 (its without spaces)
b. Then I modified code to get different rows like
0
10
210
3210
c. Modified code to get spaces at front
0
10
210
3210
d. (final output)Then I had to print digits in reverse order too, used
x for that
Code:
[CENTER] 0
101
21012
3210123
[/CENTER]
Tell me if you have any doubts
- - - Updated - - -
PS: Formatting is all messed up, the last output is the final output, I coudln't format it properly. Sorry.
- - - Updated - - -
PPS : this source code is definitely not the optimized one. I am still learning how to write optimized source codes