
Introduction
Palindrome pattern problems are very interesting to solve and easy also. A number is said to be a palindrome if it remains the same when its digits are reversed. For example, consider the number 18981. If we reverse the digits, then the number that we get after reversing the digits is 18981, which is the same as the original number. Therefore we can say that 18981 is a palindrome number.
Also read palindrome number in python.
In this article, we shall discuss various palindrome pattern problems using numbers.
Half Pyramid Palindrome
Problem Statement:
You are given a number n. Write a program to print a palindrome half pyramid containing n number of rows.
Solution in Java.
/* Java program for Palindrome half pyramid pattern printing using numbers */
import java.util.*;
public class Main {
public static void main(String[] args) {
int n, row, leftHalf, rightHalf;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows:");
n = sc.nextInt();
System.out.println();
// outer loop runs for n number of times
// here n is the number of rows
for (row = 1; row <= n; row++) {
// first inner loop
// it prints numbers from 1 to i for every ith iteration
// for e.g. in third iteration it will print 1 2 3
for (leftHalf = 1; leftHalf <= row; leftHalf++) {
System.out.print(leftHalf + "");
}
// the second inner loop
// it prints the second half of the palindrome in reverse manner
// for e.g. in third iteration it will print 2 1
// so that combining first and second inner loop
// will print 1 2 3 2 1 for the third line
for (rightHalf = row - 1; rightHalf >= 1; rightHalf--) {
System.out.print(rightHalf + "");
}
System.out.println();
}
}
}
Input :
n=6
Output:
1
121
12321
1234321
123454321
12345654321