📜  程序以明智地打印数字列

📅  最后修改于: 2022-05-13 01:57:58.962000             🧑  作者: Mango

程序以明智地打印数字列

我们必须根据给定的行数,按照以下示例中所述的给定行数,逐列打印自然数列 -

例子 :

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

方法 :

  1. 我们将采用外部 for 循环,这是我们要打印的行数。
  2. 由于我们要逐列打印自然数,为此我们将采用内部 for 循环。
    在上面的例子中,元素的数量取决于 i 的数量,因此我们取一个等于 i 的变量 k
    Value of i    ||     Number of Element
        1         ||          1
        2         ||          2
        3         ||          3     so on
    
  3. 使用逻辑k = k + n - j,我们将根据要求得到自然数。
C++
// CPP Program to natural numbers columns wise
#include 
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


输出 :

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