Program to Print Ladder Number Pattern
Cout in java: In the previous article, we have discussed Java Program to Print Solid Square Inside a Square Number Pattern
In this article we are going to see how to print ladder number program.
Example: When number of rows : 5 0 0 0 0 00000 1 1 1 1 11111 2 2 2 2 22222 3 3 3 3 33333 4 4 4 4 44444 5 5 5 5
Now, let’s see the actual program printing it.
Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.
Approach:
- Enter total row and store it in an integer variable
row
. - Take first for loop to print the row value and number for each row.
- For each iteration print 2 side numbers.
- And for each condition
(r<row)
print the stair bar. - Then go on printing the numbers according to loop.
Java Code to Print Ladder Number Pattern
import java.util.*; public class Main { public static void main(String args[]) { // taking variable for loop iteration and row . int row,r; //creating object Scanner s = new Scanner(System.in); // entering the number of row System.out.print("Enter rows : "); row = s.nextInt(); //outer for loop for ( r = 0; r <= row ; r++) { // Printing the sub-pattern 1 row+1 times System.out.println(r+" "+r); System.out.println(r+" "+r); // Printing the sub-pattern 2 row times if (r < row) System.out.println(r+""+r+""+r+""+r+r); } } }
Output: Enter rows : 5 0 0 0 0 00000 1 1 1 1 11111 2 2 2 2 22222 3 3 3 3 33333 4 4 4 4 44444 5 5 5 5
C++ Code to Print Ladder Number Pattern
#include <iostream> using namespace std; int main() { int row, r , c ,d ; cout << "Enter rows: "; cin >> row; for ( r = 0; r <= row ; r++) { cout << r<< " " << r ; cout << "\n" ; cout << r<< " " << r ; cout << "\n" ; if (r < row) cout << r<<r<<r<<r<<r ; cout << "\n" ; } return 0; }
Output: Enter rows : 5 0 0 0 0 00000 1 1 1 1 11111 2 2 2 2 22222 3 3 3 3 33333 4 4 4 4 44444 5 5 5 5
Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.
Related Java Number Pattern Programs: