Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA Tutorial
  • Data Structures
  • Algorithms
  • Array
  • Strings
  • Linked List
  • Stack
  • Queue
  • Tree
  • Graph
  • Searching
  • Sorting
  • Recursion
  • Dynamic Programming
  • Binary Tree
  • Binary Search Tree
  • Heap
  • Hashing
  • Divide & Conquer
  • Mathematical
  • Geometric
  • Bitwise
  • Greedy
  • Backtracking
  • Branch and Bound
  • Matrix
  • Pattern Searching
  • Randomized
Open In App
Next Article:
Program to Print First N Natural Numbers
Next article icon

Program to print numbers columns wise

Last Updated : 17 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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 the 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
  1. Using the logic k = k + n – j, we will get natural numbers according to requirement.

C++




// 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
 
 

Java




// 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();
    }
}
 
 

Python3




# 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
 
 

C#




//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.
 
 

PHP




<?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 
?> 
 
 

Javascript




<script>
    // Javascript Program to natural numbers columns wise
      
    function pattern(n)
    {
        // Outer loop for how many
        // lines we want to print
        for (let i = 1; i <= n; i++) {
            let k = i;
      
            // Inner loop for printing
            // natural number
            for (let j = 1; j <= i; j++) {
                document.write(k+" ");
      
                // Logic to print natural
                // value column-wise
                k = k + n - j;
            }
            document.write("<br>");
        }
          
    }
      
    // Driver Code
      
        // get value from user
        let n = 5;
          
        // function calling
        pattern(n);
          
      
    // This code is contributed by Pushpesh Raj.
      
</script>
 
 
Output
1    2 6    3 7 10    4 8 11 13    5 9 12 14 15  

Time Complexity: O(n2)
Auxiliary Space: O(1)



Next Article
Program to Print First N Natural Numbers
author
bishaldubey
Improve
Article Tags :
  • DSA
  • Basic Coding Problems
  • pattern-printing
Practice Tags :
  • pattern-printing

Similar Reads

  • Program to print numbers in digital form
    Given a number n then print the number in Digital form. Examples : Input : 5 Output : - - | - - | - - Input : 8 Output : - - | | - - | | - - Explanation: Take a matrix of size 5*5 and store 0 and 1 in the matrix. If matrix cell is 0 then it is used for space and if matrix cell is 1 then it is used e
    15+ min read
  • Program to Print Matrix in Z form
    Given a square matrix of order n*n, we need to print elements of the matrix in Z form Input: [[4, 5, 6, 8], [1, 2, 3, 1], [7, 8, 9, 4], [1, 8, 7, 5]] Output: 4 5 6 8 3 8 1 8 7 5 Input: [[4, 5, 6, 8, 5], [1, 2, 3, 1, 4], [7, 8, 9, 4, 7], [1, 8, 7, 5, 2], [7, 9, 5, 6, 9], [9, 4, 5, 6, 6]] Output: 4 5
    12 min read
  • Program to print number pattern
    We have to print a pattern where in middle column contains only 1, right side columns contain constant digit which is greater than 1 and left side columns contains constant digit which is greater than 1. Every row should look like a Palindrome. Examples : Input : 3 Output : 1 2 1 2 1 Input : 5 Outpu
    7 min read
  • Program to Print a Pattern of Numbers
    The idea of pattern based programs is to understand the concept of nesting of for loops and how and where to place the alphabets / numbers / stars to make the desired pattern.Write to program to print the pattern of numbers in the following manner using for loop 1 232 34543 4567654567898765In almost
    8 min read
  • Program to Print First N Natural Numbers
    Counting numbers like 1, 2, 3, 4, 5, 6 … Basically, all integers greater than 0 are natural numbers. Facts about Natural numbers The natural numbers are the ordinary numbers, 1, 2, 3, etc., with which we count.The next possible natural number can be found by adding 1 to the current natural numberThe
    4 min read
  • Program to print number with star pattern
    We have to print the pattern as given in the below example.Examples : Input : 5 Output : 1 1*2 1*2*3 1*2 1 Input : 9 Output : 1 1*2 1*2*3 1*2*3*4 1*2*3*4*5 1*2*3*4 1*2*3 1*2 1 C/C++ Code #include <iostream> using namespace std; // C++ program to print above pattern void display(int n) { // 'sp
    6 min read
  • Program to Print K using Alphabets
    Given a number n, the task is to print 'K' using alphabets.Examples: Input: n = 5 Output: A B C D E F A B C D E A B C D A B C A B A A A B A B C A B C D A B C D E A B C D E F Input: n = 3 Output: A B C D A B C A B A A A B A B C A B C D Below is the implementation. C/C++ Code // C++ Program to design
    5 min read
  • Program to print Even Odd Number Pyramid
    Given the total number of rows as n, the task is to print the given pattern. * 1* *2* 1*3* *2*4* 1*3*5* *2*4*6* 1*3*5*7* *2*4*6*8* 1*3*5*7*9* . . Examples: Input: n = 5 Output: * 1* *2* 1*3* *2*4* Input: n = 10 Output: * 1* *2* 1*3* *2*4* 1*3*5* *2*4*6* 1*3*5*7* *2*4*6*8* 1*3*5*7*9* Below is the sol
    4 min read
  • Program to print multiplication table of a number
    Given a number n, we need to print its table. Examples : Input: 5Output: 5 * 1 = 55 * 2 = 105 * 3 = 155 * 4 = 205 * 5 = 255 * 6 = 305 * 7 = 355 * 8 = 405 * 9 = 455 * 10 = 50 Input: 2Output: 2 * 1 = 22 * 2 = 42 * 3 = 62 * 4 = 82 * 5 = 102 * 6 = 122 * 7 = 142 * 8 = 162 * 9 = 182 * 10 = 20 Iterative Ap
    7 min read
  • Program to print pattern
    Given the value of n, print the following pattern.Examples : Input : n = 4 Output : A1 AB12 ABC123 ABCD1234 Input : n = 7 Output : A1 AB12 ABC123 ABCD1234 ABCDE12345 ABCDEF123456 ABCDEFG1234567 Below is the implementation to print the above pattern : C/C++ Code // C++ program to print given pattern
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences