Program to print numbers columns wise

We have to print natural numbers columns wise with decreasing size, depending upon given number of lines as described in the examples below –

Examples :

Input : 5
Output :
1
2 6 
3 7 10
4 8 11 13
5 9 12 14 15

Input : 3
Output :
1
2 4
3 5 6

Approach :



  1. We will take outer for loop, which is for how many lines we want to print.
  2. Since we want to print natural number column by column, for this we will take an inner for loop.
    In the above example the number of element is depending upon number for i, therefore we take a variable k which is equal to i

    Value of i    ||     Number of Element
        1         ||          1
        2         ||          2
        3         ||          3     so on
    
  3. Using the logic k = k + n – j, we will get natural numbers according to requirement.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to natural numbers columns wise
#include <bits/stdc++.h>
using namespace std;
  
void pattern(int n)
    // Outer loop for how many 
    // lines we want to print
    for (int i = 1; i <= n; i++) {
        int k = i;
  
        // Inner loop for printing
        // natural number
        for (int j = 1; j <= i; j++) {
            cout << k << " ";
  
            // Logic to print natural
            // value column-wise
            k = k + n - j;
        }
        cout << " \n";
    }
}
  
// Driver Code
int main()
{
    // get value from user
    int n = 5;
      
    // function calling
    pattern(n);
      
    return 0;
}
  
// This code is contributed by Vishal

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to natural numbers columns wise
import java.util.Scanner;
class Pattern 
{
    void display()
    {
        int n = 5;
          
        // Outer loop for how many lines we want to print
        for (int i = 1; i <= n; i++) 
        {
            int k = i;
  
            // Inner loop for printing natural number
            for (int j = 1; j <= i; j++) 
            {
                System.out.print(k);
                  
                // Logic to print natural value column-wise
                k = k + n - j;
            }
            System.out.println();
        }
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        Pattern p = new Pattern();
        p.display();
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python Program to natural numbers columns wise
  
def display():
        n = 5
        i = 1
  
        # Outer loop for how many lines we want to print
        while(i<=n): 
            k = i
            j = 1
  
            # Inner loop for printing natural number
            while(j <= i): 
                print (k,end=" ")
                  
                # Logic to print natural value column-wise
                k = k + n - j
                j = j + 1
                  
            print("\r")
            i = i + 1
  
#Driver code
display()
  
# This code is contributed by rishabh_jain

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

//C# Program to natural numbers columns wise
using System;
class Pattern 
{
    void display()
    {
        int n = 5;
          
        // Outer loop for how many lines we want to print
        for (int i = 1; i <= n; i++) 
        {
            int k = i;
  
            // Inner loop for printing natural number
            for (int j = 1; j <= i; j++) 
            {
                Console.Write(k +" ");
                  
                // Logic to print natural value column-wise
                k = k + n - j;
            }
            Console.WriteLine();
        }
    }
  
    // Driver Code
    public static void Main()
    {
        Pattern p = new Pattern();
        p.display();
    }
}
//This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation to print
// natural numbers columns wise
  
function pattern($n)
    // Outer loop for how many 
    // lines we want to print
    for ($i = 1; $i <= $n; $i++) 
    {
        $k = $i;
  
        // Inner loop for printing
        // natural number
        for ($j = 1; $j <= $i; $j++) 
        {
            echo $k." ";
  
            // Logic to print natural
            // value column-wise
            $k = $k + $n - $j;
        }
        echo " \n";
    }
}
  
// Driver Code
$n = 5;
pattern($n);
  
// This code is contributed by mits 
?> 

chevron_right


Output :

1
2 6 
3 7 10
4 8 11 13
5 9 12 14 15


My Personal Notes arrow_drop_up

Hello everyone, I am Bishal KUMAR Dubey and most importantly an idea creator I just love Programming languages and love to know new concepts everyday,every minute,every second Here to help Other GEEKS

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Mithun Kumar