Description
Get the number of rows as input from user and print the following pattern.
Input
5
Output
1
123
12345
1234567
123456789
1234567
12345
123
1
C Program
#include <stdio.h>
int main()
{
int n, i, j;
printf("Enter number of rows: ");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
for(j=1;j<=i*2-1;j++)
{
printf("%d",j);
}
printf("\n");
}
for(i=n-1;i>0;i--)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
for(j=1;j<=i*2-1;j++)
{
printf("%d",j);
}
printf("\n");
}
return 0;
}
C++ Program
#include <iostream>
using namespace std;
int main()
{
int n, i, j;
cout<<"Enter number of rows: ";
cin>>n;
for(i=1; i<=n; i++)
{
for(j=1;j<=n-i;j++)
{
cout<<" ";
}
for(j=1;j<=i*2-1;j++)
{
cout<<j;
}
cout<<"\n";
}
for(i=n-1;i>0;i--)
{
for(j=1;j<=n-i;j++)
{
cout<<" ";
}
for(j=1;j<=i*2-1;j++)
{
cout<<j;
}
cout<<"\n";
}
return 0;
}
Java Program
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int rows= sc.nextInt();
for(int i=1;i<=rows;i++)
{
for(int j=1;j<=rows-i;j++)
{
System.out.print(" ");
}
for(int j=1;j<=i*2-1;j++)
{
System.out.print(j);
}
System.out.println();
}
for(int i=rows-1;i>0;i--)
{
for(int j=1;j<=rows-i;j++)
{
System.out.print(" ");
}
for(int j=1;j<=i*2-1;j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
Python Program
row=int(input("Enter the number of rows: "))
for i in range(row):
x=1
for j in range(2*row-1):
if(i+j<row-1 or i+j>=row+2*i):
print(" ",end="")
else:
print(x,end="")
x+=1
print()
for i in range(row-1):
x=1
for j in range(2*row-1):
if(i+j<=2*i or i+j>=(2*row-2)):
print(" ",end="")
else:
print(x,end="")
x+=1
print()