Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Half Pyramid Palindrome
3.
Pyramid Palindrome
4.
Pyramid Palindrome Using Number and Stars
5.
Frequently Asked Questions
5.1.
What is a palindrome?
5.2.
What are for loops?
5.3.
What are palindrome patterns?
5.4.
Give an example of a palindrome number.
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

Palindrome Pattern

Author ANKIT KUMAR
1 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @
Interview Puzzles

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
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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();


        }


    }
}

Input:

n=7

Output:  

      1
     121
    12321
   1234321
  123454321
 12345654321
1234567654321

 

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

Output:

********1********
*******2*2*******
******3*3*3******
*****4*4*4*4*****

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;
        }
    }
}

Input:

n=7

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**

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.

Previous article
Matrix in Zig-Zag Form
Next article
Inverted V using Asterisk Pattern
Live masterclass