Program to print Diamond pattern using Row numbers

Program to print Diamond pattern using Row numbers

21 April 2023

21 April 2023

Write a program to print Diamond pattern using Row numbers



If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Description

Get the number of rows from user and the print the pattern as follows

Input

3

Output

   1

  22

 333

  22

   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",i);

        }

        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",i);

        }

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

        }

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

        }

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

                }

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

                  }

                  System.out.println();

             }

                }

}

 

Python Program

row=int(input("Enter the number of rows: "))

for i in range(row):

    for j in range(2*row-1):

        if(i+j<row-1 or i+j>=row+2*i):  

            print(" ",end="")

        else:

            print(i+1,end="")

    print()

for i in range(row-1):

    for j in range(2*row-1):

        if(i+j<=2*i or i+j>=(2*row-2)):     

            print(" ",end="")

        else:

            print(row-1-i,end="")

    print()


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023


Related Articles

Ask Us Anything !