Description
Get the number of rows from the user and print the palindromic pattern as given below.
Input
3
Output
1
121
12321
C Program
#include<stdio.h>
int main()
{
int rows;
printf("Enter the row number:");
scanf("%d",&rows);
for(int i=0;i<=rows;i++)
{
for(int k=i;k<=rows;k++)
printf(" ");
for(int j=1;j<=i;j++)
printf("%d",j);
for(int j=i-1;j>0;j--)
printf("%d",j);
printf("\n");
}
}
C++ Program
#include <iostream>
using namespace std;
int main()
{
int rows;
cout<<"Enter the row number: ";
cin>>rows;
for(int i=0;i<=rows;i++)
{
for(int k=i;k<=rows;k++)
cout<<" ";
for(int j=1;j<=i;j++)
cout<<j;
for(int j=i-1;j>0;j--)
cout<<j;
cout<<"\n";
}
}
Java
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
for(int i=0;i<=rows;i++)
{
for(int k=i;k<=rows;k++)
System.out.print(" ");
for(int j=1;j<=i;j++)
System.out.print(j);
for(int j=i-1;j>0;j--)
System.out.print(j);
System.out.println();
}
}
}
Python
def palindrome(rows):
for i in range(0,rows+1):
for j in range(i,rows+1):
print(" ",end=" ")
for j in range(1,i+1):
print(j,end=" ")
for j in range(i-1,0,-1):
print(j,end=" ")
print()
rows=int(input("Enter the number of rows :"))
palindrome(rows)