10 Java Number Patterns Programs for Interview. Elevate your interview performance and showcase your problem-solving skills effortlessly.
1.Half Pyramid of Numbers – Number Patterns Program
1 12 123 1234 12345
public class HalfPyramidNumbers { public static void main(String[] args) { int rows = 5; for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print(j + " "); } System.out.println(); } } }
Here's a step-by-step explanation:
- Class Declaration:
- The program starts with the declaration of a public class named
HalfPyramidNumbers
.
- The program starts with the declaration of a public class named
- Main Method:
- Inside the
HalfPyramidNumbers
class, there's amain
method, which serves as the entry point of the program. It is declared aspublic static void main(String[] args)
.
- Inside the
- Variable Declaration:
- In the
main
method, an integer variablerows
is declared and initialized with the value5
. This variable represents the number of rows in the pattern to be printed.
- In the
- Outer Loop (Rows):
- The program uses a
for
loop to iterate over the rows of the pattern. It starts withi = 1
and continues as long asi
is less than or equal torows
. In each iteration,i
is incremented by1
.
- The program uses a
- Inner Loop (Columns):
- Inside the outer loop, there's another
for
loop responsible for printing the numbers in each row of the pattern. It starts withj = 1
and continues as long asj
is less than or equal toi
(the current row number). In each iteration,j
is incremented by1
.
- Inside the outer loop, there's another
- Print Numbers:
- Inside the inner loop, the program prints the value of
j
followed by a space usingSystem.out.print(j + " ")
. This prints the numbers from1
up to the value ofi
in each row, separated by spaces.
- Inside the inner loop, the program prints the value of
- New Line:
- After printing all the numbers for a particular row, the program moves to the next line using
System.out.println()
to start a new row.
- After printing all the numbers for a particular row, the program moves to the next line using
- Repeat:
- The outer loop continues to execute until
i
reaches the value ofrows
, printing one row at a time with increasing numbers in each row.
- The outer loop continues to execute until
2.Inverted Half Pyramid of Numbers
1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
public class InvertedHalfPyramidNumbers { public static void main(String[] args) { int rows = 5; for (int i = rows; i >= 1; i--) { for (int j = 1; j <= i; j++) { System.out.print(j + " "); } System.out.println(); } } }
Here's a step-by-step explanation:
- Class Declaration: The program begins with the declaration of a public class named
InvertedHalfPyramidNumbers
. This class contains themain
method, which serves as the entry point of the program. - Main Method: Inside the
main
method, the program's execution starts. This method is declared aspublic static void
, indicating that it can be accessed by other classes without needing to instantiate an object of the class (static
), and it doesn't return any value (void
). - Variable Initialization: Within the
main
method, an integer variablerows
is declared and initialized with the value5
. This variable represents the number of rows in the inverted half pyramid. - Outer Loop (i): The program uses a
for
loop to iterate over the rows of the inverted half pyramid. The loop starts with the value ofi
set to the value ofrows
(5 in this case), and it decrementsi
by 1 in each iteration untili
becomes 0. This loop controls the number of rows in the inverted half pyramid. - Inner Loop (j): Within the outer loop, another
for
loop is used to print the numbers in each row. This loop starts with the value ofj
set to1
and incrementsj
by 1 in each iteration untilj
becomes equal toi
. This loop controls the number of columns in each row and prints the numbers from1
toi
. - Print Statement: Inside the inner loop, the program prints the value of
j
followed by a space usingSystem.out.print()
. This prints the numbers in the pattern required for the inverted half pyramid. - New Line: After printing the numbers for each row, the program moves to a new line using
System.out.println()
. This results in the numbers being printed in different rows, forming the inverted half pyramid pattern. - End of Inner Loop: Once the inner loop completes its execution for each row, the program moves to the next iteration of the outer loop (if applicable), where
i
is decremented, and the process repeats until all rows are printed. - End of Program: After all rows are printed, the program execution completes, and the program terminates.
Overall, this program generates an inverted half pyramid of numbers with each row containing numbers from 1 to the row number. The number of rows in the pyramid is specified by the rows
variable, which is currently set to 5
.
3. Floyd's Triangle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
public class FloydsTriangle { public static void main(String[] args) { int rows = 5; int num = 1; for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print(num + " "); num++; } System.out.println(); } } }
Here's a step-by-step explanation:
- Class Declaration:
- The program starts with declaring a public class named
FloydsTriangle
. In Java, the name of the class must match the name of the file containing it.
- The program starts with declaring a public class named
- Main Method:
- Inside the
FloydsTriangle
class, there is amain
method. This method is the entry point of the program, where execution begins.
- Inside the
- Variable Initialization:
- Two integer variables
rows
andnum
are initialized.rows
stores the number of rows in the triangle pattern, andnum
is used to generate the numbers for the triangle.
- Two integer variables
- Nested Loop:
- Two nested
for
loops are used to print the triangle pattern. - The outer loop (
for (int i = 1; i <= rows; i++)
) controls the number of rows in the triangle. It iterates from 1 to the value ofrows
. - The inner loop (
for (int j = 1; j <= i; j++)
) controls the number of elements in each row. It iterates from 1 to the current value ofi
. - Inside the inner loop, the value of
num
is printed followed by a space, and thennum
is incremented. - After printing all elements of a row, a newline character (
System.out.println()
) is printed to move to the next row.
- Two nested
- Output:
- The program prints a Floyd's Triangle pattern with
5
rows. Floyd's Triangle is a right-angled triangular array of natural numbers, used in mathematics and computer science education. Each row contains consecutive natural numbers, starting from 1 and increasing by 1 in each subsequent row.
- The program prints a Floyd's Triangle pattern with
- End of Program:
- After printing the triangle pattern, the
main
method ends, and the program terminates.
- After printing the triangle pattern, the
Explore More Star Pattern Interview Questions: Click Here
4. Pyramid of Numbers
1 1 2 3 1 2 3 4 5 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 9
public class PyramidNumbers { public static void main(String[] args) { int rows = 5, k = 0; for (int i = 1; i <= rows; i++, k = 0) { for (int space = 1; space <= rows - i; space++) { System.out.print(" "); } while (k != 2 * i - 1) { System.out.print((k + 1) + " "); k++; } System.out.println(); } } }
Here's a step-by-step explanation:
- The program begins with the declaration of a public class named
PyramidNumbers
. - Inside the class, there is a
main
method, which serves as the entry point for the program. - In the
main
method, two integer variables are declared and initialized:rows
is set to 5, indicating the number of rows in the pyramid.k
is initialized to 0. This variable is used to keep track of the numbers to be printed in each row.
- The program uses a nested loop structure to generate the pyramid pattern. The outer loop iterates over each row of the pyramid, starting from row 1 and ending at row
rows
. - Within the outer loop, a nested loop is used to print spaces before printing the numbers. The number of spaces decreases as the row number increases, creating the pyramid shape. This loop runs from
1
torows - i
, wherei
represents the current row number. - Inside the nested loop,
System.out.print(" ")
prints two spaces for each space required in the pyramid. This ensures proper alignment of the numbers. - After printing the spaces, a
while
loop is used to print the numbers for each row. This loop runs untilk
becomes equal to2 * i - 1
, wherei
represents the current row number. In each iteration of this loop, it prints the value ofk + 1
, followed by a space. - The variable
k
is incremented in each iteration of thewhile
loop to ensure that the correct sequence of numbers is printed for each row. - Once all the numbers for the current row have been printed, a new line is printed using
System.out.println()
to move to the next row. - This process repeats until all the rows of the pyramid have been printed.
5. Pascal's Triangle
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
public class PascalsTriangle { public static void main(String[] args) { int rows = 5; for (int i = 0; i < rows; i++) { int number = 1; System.out.format("%" + (rows - i) * 2 + "s", ""); for (int j = 0; j <= i; j++) { System.out.format("%4d", number); number = number * (i - j) / (j + 1); } System.out.println(); } } }
Here's a step-by-step explanation:
- Class Declaration: The program starts with the declaration of a public class named
PascalsTriangle
. - Main Method: Inside the class, there's the
main
method, which serves as the entry point for the program execution. - Variable Initialization: Within the
main
method, an integer variablerows
is initialized with a value of 5. This variable determines the number of rows in Pascal's triangle that will be printed. - Outer Loop (Row Generation): The program uses a
for
loop to iterate over each row of Pascal's triangle. It starts fromi = 0
and continues untili < rows
. - Space Formatting: Inside the outer loop, there's a line to format spaces before printing the numbers. It dynamically adjusts the number of spaces based on the current row (
rows - i
), multiplying it by 2 and formatting it as a string withSystem.out.format
. - Inner Loop (Printing Numbers): Within the outer loop, there's another
for
loop to generate and print the numbers for each row of the triangle. It starts fromj = 0
and continues untilj <= i
, wherei
represents the current row number. - Number Calculation: Inside the inner loop, a variable named
number
is initialized with a value of 1. This variable holds the value of each number in the current row of Pascal's triangle. - Printing Numbers: Using
System.out.format
, the program prints each number with a width of 4 characters (%4d
). This ensures that each number is properly aligned in the triangle. - Number Calculation (Cont.): After printing each number, the program calculates the next number in the row using the formula
number = number * (i - j) / (j + 1)
. This formula calculates each number based on the previous number and its position in the row. - New Line: After printing all the numbers for a row, the program prints a newline character (
System.out.println()
) to move to the next row. - Loop Continuation: The outer loop continues until all rows of Pascal's triangle are generated and printed.
- End of Program: Once all rows are printed, the
main
method ends, and consequently, the program terminates.
6. Number Pattern with Spaces
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
public class NumberPatternWithSpaces { public static void main(String[] args) { int rows = 5; for (int i = 1; i <= rows; i++) { for (int j = 1; j <= rows - i; j++) { System.out.print(" "); } for (int k = 1; k <= i; k++) { System.out.print(k + " "); } System.out.println(); } } }
Here's a step-by-step explanation:
- Class Declaration:
- The program begins with the declaration of a public class named
NumberPatternWithSpaces
.
- The program begins with the declaration of a public class named
- Main Method:
- Inside the class, there is a
main
method, which is the entry point of the program.
- Inside the class, there is a
- Variable Initialization:
- In the
main
method, an integer variablerows
is initialized with a value of5
. This variable determines the number of rows in the pattern.
- In the
- Outer Loop:
- The program uses a
for
loop with a control variablei
to iterate over each row of the pattern. It starts from1
and continues untili
reaches the value ofrows
.
- The program uses a
- Inner Loop for Spaces:
- Inside the outer loop, there is another
for
loop with control variablej
. This loop is responsible for printing the spaces before the numbers in each row. - The number of spaces decreases as
i
increases. This is achieved by iterating from1
torows - i
.
- Inside the outer loop, there is another
- Printing Spaces:
- Within the inner loop,
System.out.print(" ")
is used to print two spaces without advancing to a new line. This creates the required spacing before the numbers.
- Within the inner loop,
- Inner Loop for Numbers:
- After printing the spaces, there is another
for
loop with control variablek
, responsible for printing the numbers in each row. - This loop starts from
1
and continues untilk
reaches the value ofi
. - The loop prints the numbers from
1
toi
, separated by a space.
- After printing the spaces, there is another
- Printing Numbers:
- Inside the inner loop,
System.out.print(k + " ")
is used to print the current value ofk
followed by a space.
- Inside the inner loop,
- Newline:
- After printing the numbers for each row,
System.out.println()
is used to move to the next line, starting a new row in the pattern.
- After printing the numbers for each row,
- Loop Continues:
- The outer loop continues to iterate until
i
reaches the value ofrows
, printing each row of the pattern with appropriate spaces and numbers.
- The outer loop continues to iterate until
- Program Completion:
- Once the outer loop finishes executing, the
main
method concludes, and the program execution ends.
- Once the outer loop finishes executing, the
7. Number Pattern in Reverse
5 4 3 2 1 5 4 3 2 5 4 3 5 4 5
public class NumberPatternInReverse { public static void main(String[] args) { int rows = 5; for (int i = 1; i <= rows; i++) { for (int j = rows; j >= i; j--) { System.out.print(j + " "); } System.out.println(); } } }
Here's a step-by-step explanation:
- Class Declaration: The program starts with the declaration of a public class named
NumberPatternInReverse
. In Java, every application begins with a class definition. - Main Method: Within the
NumberPatternInReverse
class, there is amain
method. This method serves as the entry point of the program, where the execution begins. - Variable Declaration and Initialization: Inside the
main
method, an integer variable namedrows
is declared and initialized with the value5
. This variable represents the number of rows in the pattern to be printed. - Outer Loop: The program uses a
for
loop to iterate from1
torows
. This loop controls the number of rows in the pattern. - Inner Loop: Within the outer loop, there is another
for
loop. This loop iterates fromrows
down toi
. It controls the number of elements to be printed in each row, and it starts fromrows
and decrements by1
in each iteration. - Printing Numbers: Inside the inner loop, the program prints the value of
j
followed by a space. The value ofj
represents the numbers to be printed in each row. Since the inner loop iterates fromrows
down toi
, it prints the numbers in reverse order in each row. - Newline: After printing the numbers for each row, the program prints a newline character (
\n
) to move to the next line and start a new row. - End of Inner Loop: Once the inner loop completes its iterations for a particular row, the control goes back to the outer loop, and the next iteration begins.
- End of Program: Once all the iterations of the outer loop are completed, the program execution ends.
8. Right Triangle of Numbers
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
public class RightTriangleNumbers { public static void main(String[] args) { int rows = 5; for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print(j + " "); } System.out.println(); } } }
Here's a step-by-step explanation:
- Class Declaration: The program starts with the declaration of a public class named
RightTriangleNumbers
. In Java, every application must have at least one class definition, and the name of the file containing the class must match the name of the class (in this case, it should be saved in a file namedRightTriangleNumbers.java
). - Main Method: Inside the class, there's a
main
method, which serves as the entry point of the program. It is declared with thepublic static void
keywords, indicating that it can be accessed from outside the class without creating an object of the class (public
), it can be called without creating an instance of the class (static
), and it does not return any value (void
). Themain
method takes an array of strings (String[] args
) as its parameter, which allows the program to accept command-line arguments. - Variable Initialization: Within the
main
method, an integer variable namedrows
is initialized with the value5
. This variable determines the number of rows in the right triangle pattern. - Outer Loop (Rows): The program then enters a loop that iterates from
1
to the value ofrows
(inclusive). This loop controls the number of rows in the right triangle pattern. - Inner Loop (Columns): Inside the outer loop, there's another loop that iterates from
1
to the current value ofi
(inclusive). This inner loop controls the number of columns (or numbers) printed in each row of the pattern. - Printing Numbers: Within the inner loop, the program prints the current value of
j
followed by a space usingSystem.out.print(j + " ")
. This prints the numbers1
,2
,3
, ..., up to the current value ofi
, separated by spaces, in each row of the pattern. - New Line: After printing all the numbers for the current row, the program prints a newline character (
\n
) usingSystem.out.println()
. This moves the cursor to the next line, creating a new row in the pattern. - Loop Execution: The outer loop continues to iterate until the value of
i
reaches the value ofrows
, and the inner loop executes its iterations accordingly. - Program Completion: Once the outer loop finishes executing, the
main
method ends, and consequently, the program terminates.
Explore More Star Pattern Interview Questions: Click Here