Pyramid Palindrome
Problem Statement:
You are given a number n. Write a program to print a palindrome pyramid containing n number of rows.
Solution in Java.
import java.util.*;
public class Main {
public static void main(String[] args) {
int n, i, j, k, l;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
n = sc.nextInt();
// outer loop
// it runs for n number of times where n is the number of rows
for (i = 1; i <= n; i++) {
// first inner loop
// it is used to print the preceding white spaces
for (j = 1; j <= n - i; j++)
System.out.print(" ");
// second inner loop
// it prints the first half of the palindrome
// for e.g. in fourth iteration it prints 1234
for (k = 1; k <= i; k++) {
System.out.print(k);
}
// third inner loop
// it prints the second half of the palindrome
// for e.g. in fourth iteration it prints 321
for (l = i - 1; l >= 1; l--) {
System.out.print(l);
}
// move to new line
System.out.println();
}
}
}

You can also try this code with Online Java Compiler
Run Code
Input:
n=7

You can also try this code with Online Java Compiler
Run Code
Output:
1
121
12321
1234321
123454321
12345654321
1234567654321

You can also try this code with Online Java Compiler
Run Code
Must Read C Program to Reverse a Number
Pyramid Palindrome Using Number and Stars
Problem Statement:
You are given a number n. Write a program to print a palindrome pyramid using numbers and stars containing n number of rows.
Example:
Input:
n=4

You can also try this code with Online Java Compiler
Run Code
Output:
********1********
*******2*2*******
******3*3*3******
*****4*4*4*4*****

You can also try this code with Online Java Compiler
Run Code
Solution in Java.
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows: ");
int i, j, space, count = 1, num = 0;
int n = sc.nextInt();
// maximum eight stars to the left and right
int star = 8;
space = n;
// outer loop
for (i = 1; i <= n; i++) {
// first inner loop
// it takes care of the left part of the pattern
for (j = 1; j <= star; j++){
if (i + j <= star + 1)
System.out.print("*");
}
// increment the num count before printing
num++;
// second inner loop
// it takes care of the right part of the pattern
for (j = 1; j <= i; j++) {
System.out.print(num);
if (i > 1 && count < i) {
System.out.print("*");
count++;
}
}
for (j = 1; j <= star; j++){
if (i + n <= j + n)
System.out.print("*");
}
System.out.println();
space--;
count = 1;
}
}
}

You can also try this code with Online Java Compiler
Run Code
Input:
n=7

You can also try this code with Online Java Compiler
Run Code
Output:
********1********
*******2*2*******
******3*3*3******
*****4*4*4*4*****
****5*5*5*5*5****
***6*6*6*6*6*6***
**7*7*7*7*7*7*7**

You can also try this code with Online Java Compiler
Run Code
Check out this problem - Check If A String Is Palindrome
You can also read about Palindrome Number in Python here.
Frequently Asked Questions
What is a palindrome?
A number is said to be a palindrome if it remains the same when its digits are reversed.
What are for loops?
A "for" Loop is used to repeat a specific block of code a certain number of times.
What are palindrome patterns?
These are the pattern problems that generally involve palindromic sequences.
Give an example of a palindrome number.
1356531.
Conclusion
In this article, we discussed what a palindrome number is and learned how to print various palindromic patterns involving numbers. A number is said to be a palindrome if it remains the same when its digits are reversed.
Recommended Readings:
Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, etc. on Coding Ninjas Studio.
Also check out some of the Guided Paths on topics such as Interview Puzzles, Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.