Hello Readers,

In this post we are going to see, how to print DIAMOND PATTERN OF NUMBERS in C++, JAVA, PYTHON which is as follows:-


Logic to print the "DIAMOND" Pattern of numbers:-

1.Assign seven variables as row, i , j, k, l ,m, n.

2.Now use the variable row to take the input from the user i.e. the number of rows

3.Now using "for loop" start the iterator(i) from 1 till the no of rows user want i.e.(n) use post incrementation for 'i' value (This Loop is for row management)

4.Using nested for loop(loop inside a loop) take another iterator as 'j' and initialize it as input - 1 i.e.(n-1), in this, loop will check everytime whether it is greater than or equal to 1 i.e. (j>=1),use post decrementation for 'j'.

5.Now inside this loop we will print Nothing i.e.(" ") for space between two columns.

6.Outside of this loop make another loop and use 'k' as its iterator, start the value of 'k' with 1, till (2 * i - 1), use post incrementation for 'k'

7.Now inside this loop print the value of 'i'.

8.Outside this loop print the code for next line i.e."\n" .

9. Now close the First loop in which all the above loops were nested i.e. loop for 'i'.

10.Now use another for loop with iterator 'l' (From this part, program will print the bottom part of the diamond)

11.initialize the value of 'l' as input-1 i.e.(num-1), the loop will check whether the value is greater than or equal to 1 or not, use post decrementation for l (l--).

12. using Nested for loop, take another iterator as 'm', initialize it as (num-1),the loop will also check whether the value of 'm' is greater than or equal to 1 or not, use post decrementation for m (m--).

13.Now inside this loop we will print Nothing i.e.(" ") for space between two columns.

14. Outside this loop take another loop with iterator 'n', where its value will start from 1, its value should be greater than (l*2) if not then this loop will be terminated, use post incrementation for 'n'.

15.Inside this loop print the value of 'l'

16.Outside this loop print the code for next line i.e."\n" .

17. Now close the First loop in which all the above loops were nested i.e. loop for 'l'.

18.In this way you can print the "DIAMOND PATTERN".

DIAMOND PATTERN:

      1
    222
  33333
    222
      1



C


 //**HELPFORCODERS**
 //This is a Pattern project which prints
 //    1
 //   222
 //  33333
 // 4444444
 //  33333
 //   222
 //    1

 #include<stdio.h>
 int main()
 {
    int n;
    printf("Enter the number of Rows:\n");
    scanf("%d",&n);
    int i, j, k;
    for (i = 1; i <= n; i++)
    {
        for (j = n-1; j >= i; j--)
        {
           printf(" ");
        }
        for (k = 1; k <= (2 * i - 1); k++)
        {
           printf("%d",i);
        }
        printf("\n");
    }
 
    int l, m,z;
    for (l = n-1; l >= 1; l--)
    {
        for (m = n-1; m >= l; m--)
        {
            printf(" ");
        }
        for (z = 1; z < (l * 2); z++)
        {
            printf("%d", l);
        }
        printf("\n");
    }
 
    return 0;
 }

    


Output For C


C++


 //**HELPFORCODERS**
 //This is a Pattern project which prints
 //    1
 //   222
 //  33333
 // 4444444
 //  33333
 //   222
 //    1

 #include<iostream>
 using namespace std;

 int main()
 {
     int num;
     cout<<"Enter the number of Rows:\n";
     cin>>num;
     int i, j, k;
     for (i = 1; i <= num; i++)
     {
         for (j = num-1; j >= i; j--)
         {
             cout << " ";
         }
         for (k = 1; k <= (2 * i - 1); k++)
         {
             cout << i;
         }
         cout << "\n";
     }

     int l, m, n;
     for (l = num-1; l >= 1; l--)
     {
         for (m = num-1; m >= l; m--)
         {
             cout << " ";
         }
         for (n = 1; n < (l * 2); n++)
         {
             cout << l;
         }
         cout << "\n";
     }

     return 0;
 }
 
    


Output For C++


Java


 //**HELPFORCODERS**
 //This is a Pattern project which prints
 //    1
 //   222
 //  33333
 // 4444444
 //  33333
 //   222
 //    1

 package com.company;
 import java.util.*;

 public class Main
 {
     public static void main(String[] args)
     {

         System.out.println("Enter the number of rows");
         Scanner input = new Scanner(System.in);
         int num = input.nextInt();

         for (int i = 1; i <= num; i++)
         {
             for (int j = num-1; j >= i; j--)
             {
                 System.out.print(" ");
             }
             for (int k = 1; k <= (2 * i - 1); k++)
             {
                 System.out.print(i);
             }
             System.out.print("\n");
         }

         for (int l = num-1; l >= 1; l--)
         {
             for (int m = num-1; m >= l; m--)
             {
                 System.out.print(" ");
             }
             for (int n = 1; n < (l * 2); n++)
             {
                 System.out.print(l);
             }
             System.out.print("\n");
         }

     }
 }


Output for JAVA


Python


 #**HELPFORCODERS**
 #This is a Pattern project which prints
 #    1
 #   222
 #  33333
 # 4444444
 #  33333
 #   222
 #    1

 rows = int(input("Enter the number of Rows\n"))

 n = 0

 for i in range(1, rows + 1):
     for j in range(1, (rows - i) + 1):
         print(end=" ")
     # loop to print star
     while n != (2 * i - 1):
         print(i, end="")
         n = n + 1
     n = 0
     print("\r")

 k = 1
 n = 1

 for i in range(1, rows):
     # loop to print spaces
     for j in range(1, k + 1):
         print(end=" ")
     k = k + 1
     # loop to print star
     while n <= (2 * (rows - i) - 1):
         print(rows - i, end="")
         n = n + 1
     n = 1
     print("\r")


Output for PYTHON

//ENJOY CODING\\



Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post