Prime #### Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime #### Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

# Pyramid number pattern

## Pyramid number pattern

In this section, we learn how to print pyramid number pattern in the java.

here  we create half pyramid, inverted half pyramid and the triangle pyramid using a number in java.

this pattern example are useful also in interview question in java, so see every step carefully of each pattern.

To create a simple number pattern using , you have to use two loop. Due to this, nested for loop define rows to create with the columns of the number.

The first for loop define the number of rows for the pattern. The second Loop prints each row with the number. ### 1. Write the Program to print the following pattern

`Enter the row 511 21 2 3 1 2 3 41 2 3 4 5`

### Working

Step 1-  first we need to initialize two variable for loop.

Step 2- The first loop work for the row. so loop work from 1 to row;

Step 3- The second loop work for the number in a row. so this loop work from 1 to i.

Step 4. Inside the second loop, print the j.

Step 5- Outside the second loop and inside the first loop, give new line.

Step 6- Stop.

`#include <stdio.h>  int main()  {    int i,  j, n;    printf("Enter number of rows : ");    scanf("%d",&n);    for(i=1; i<=n; i++)    {        for(j=1; j<=i; j++)        {           printf("%d ",j);        }        printf("\n");    }        return 0;}  `
`//java program to print pyramid of the numberimport java.util.Scanner;public class Main{ public static void main(String[] args)     {         int i,j;         //Scanner class declaration          Scanner sc = new Scanner(System.in);         //input from user         System.out.print(" Enter the row");         int row = sc.nextInt();         //first loop for the row         for(i=1;i<=row;i++)         {             //second loop for print element in the row             for(j=1;j<=i;j++)                System.out.print(j+" ");             System.out.println();         }     }}`
n=5
for i in range(n):
for j in range(i+1):
print(j+1,end=” “)
print()
`#include<iostream>using namespace std;  int main()  {    int i,  j, n;    cin>>n;    for(i=1; i<=n; i++)    {        for(j=1; j<=i; j++)        cout<<j;                        cout<<endl;    }        return 0;}  `

### 2. Write a program to print the following pattern

`Enter number  51 2 3 4 51 2 3 41 2 31 2 1`
`#include <stdio.h>  int main()  {    int i,  j, n;    printf("Enter number of rows : ");    scanf("%d",&n);    for(i=n; i>=1; i--)    {        for(j=1; j<=i; j++)        {            printf("%d ",j);        }        printf("\n");    }       return 0;}  `
`//java program to print patternimport java.util.Scanner;public class Main{ public static void main(String[] args)  {         int i,j;         //scanner class declaration         Scanner sc = new Scanner(System.in);         //input from user         System.out.print(" Enter the row");         int row = sc.nextInt();         //first loop for every new row         for(i=row;i>=0;i--)         {             //second loop to print number in the row             for(j=1;j<=i;j++)                System.out.print(j+" ");             System.out.println();         }   }}`
n=5
for i in range(n):
for j in range(n-i):
print(j+1,end=” “)
print()
`#include<iostream>using namespace std;int main()  {    int i,  j, n;    cin>>n;        for(i=n; i>=1; i--)    {        for(j=1; j<=i; j++)        {            cout<<j;        }        cout<<endl;    }       return 0;}`

### 3. Write a program to print the following pattern

`Enter the row 5             1           2 3 2         3 4 5 4 3       4 5 6 7 6 5 4     5 6 7 8 9 8 7 6 5`
`#include <stdio.h>  int main()  {    int n, i, j, k=0, s, count=0, count1=0;    printf("Enter number of rows : ");    scanf("%d",&n);    for(i=1; i<=n; i++)    {        for(s=1; s<=n-i; s++)        {            printf("  ");            count++;        }        while(k!=2*i-1)        {            if(count<=n-1)            {                printf("%d ",k+i);                count++;            }            else            {                count1++;                printf("%d ",i+k-2*count1);            }            k++;        }        count1 = count = k = 0;        printf("\n");    }       return 0;}  `
`import java.util.Scanner;public class Main{ public static void main(String[] args)     {         int i,j,k=0,s,count=0,count1=0;         //scanner class declaratiom         Scanner sc = new Scanner(System.in);         //input from user         System.out.println("Enter the row");         int row = sc.nextInt();         //this loop work for the every new row         for(i=1;i<=row;i++)         {             //this loop is for spaces             for(s=1;s<=row-i;s++)             {                System.out.print("  ");                count++;             }             //while loop for print the number in the row             while(k!=2*i-1)             {                 if(count<=row-1)                 {                     System.out.println((k+i)+" ");                     count++;                 }                 else                 {                     count1++;                     System.out.println((i+k-2*count1)+" ");                 }                 k++;             }             count1=count=k=0;             System.out.println();        }      }}`
n=5
for i in range(n):
x=i+1
for j in range(2*n-1):
if(i+j<n-1 or i+j>=n+2*i):
print(” “,end=“”)
else:
print(x,end=“”
if(j<n-1):
x+=1
else:
x-=1
print()
`#include <iostream>using namespace std;int main()  {    int n, i, j, k=0, s, count=0, count1=0;        cin>>n;        for(i=1; i<=n; i++)    {        for(s=1; s<=n-i; s++)        {            cout<<" ";            count++;        }        while(k!=2*i-1)        {            if(count<=n-1)            {                cout<<k+i;                count++;            }            else            {                count1++;                cout<<i+k-2*count1;            }            k++;        }        count1 = count = k = 0;        cout<<endl;    }       return 0;}  `