📜  打印数字图案的程序 |套装 – 2

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

打印数字图案的程序 |套装 – 2

给定一个数字为“num”,行数为“num_of_lines”,其中“num”表示模式必须从其开始的起始编号,“num_of_lines”表示必须打印的行数。现在,根据上述信息,打印如下所示的图案。
例子:

Input: num = 7, num_of_lines = 4
Output: 7
        14 15
        28 29 30 31
        56 57 58 59 60 61 62 63

Input: num = 3, num_of_lines = 3
Output: 3
        6 7
        12 13 14 15

观察:

  • 第一列的元素是该列中前一个元素的倍数。
  • 每行中的元素数量是数量的两倍。上一行中的元素。
  • 此外,要生成行中的下一个元素,请将 1 添加到该行的前一个元素。

方法:
因此,开始一个从 0 到 num_of_lines-1 的循环,以处理要打印的行数和第一个循环内的另一个循环,从 0 到 limit-1,limit 将初始化为 1,其值为成倍增长。现在在循环内部,只需将数字加 1 即可打印该行的下一个数字。

C++
// C++ program to print the
// given numeric pattern
#include 
using namespace std;
 
// Function to print th epattern
void printPattern (int num, int numOfLines )
{
    int n = num, num2, x = 1, limit = 1;
 
    // No. of rows to be printed
    for (int i = 0; i < numOfLines; i++) {
         
        // No. of elements to be printed in each row
        for (int j = 0; j < limit; j++) {
            if (j == 0)
                num2 = num;
 
            // Print the element
            cout << num2++ << " ";
        }
        num *= 2;
        limit = num / n;
        cout << endl;
    }
}
 
// Drivers code
int main()
{
 
    int num = 3;
    int numOfLines = 3;
 
    printPattern(num,  numOfLines);
     
    return 0;
}


Java
// Java program to print the
// given numeric pattern
class solution_1
{
     
// Function to print
// the pattern
static void printPattern (int num,
                          int numOfLines)
{
    int n = num, num2 = 0,
        x = 1, limit = 1;
 
    // No. of rows to
    // be printed
    for (int i = 0;
             i < numOfLines; i++)
    {
         
        // No. of elements to be
        // printed in each row
        for (int j = 0; j < limit; j++)
        {
            if (j == 0)
                num2 = num;
 
            // Print the element
            System.out.print(num2++ + " ");
        }
         
        num *= 2;
        limit = num / n;
        System.out.println();
    }
}
 
// Driver code
public static void main(String args[])
{
    int num = 3;
    int numOfLines = 3;
 
    printPattern(num, numOfLines);
}
}
 
// This code is contributed
// by Arnab Kundu


Python 3
# Python 3 program to print
# the given numeric pattern
 
# Function to print th epattern
def printPattern (num, numOfLines ):
 
    n = num
    limit = 1
 
    # No. of rows to be printed
    for i in range(0, numOfLines):
         
        # No. of elements to be
        # printed in each row
        for j in range(limit):
            if j == 0:
                num2 = num
 
            # Print the element
            print(num2, end = " ")
            num2 += 1
     
        num *= 2
        limit = num // n
        print()
         
# Driver code
if __name__ == "__main__":
 
    num = 3
    numOfLines = 3
 
    printPattern(num, numOfLines)
 
# This code is contributed
# by ChitraNayal


C#
// C# program to print the
// given numeric pattern
using System;
class GFG
{
     
// Function to print
// the pattern
static void printPattern(int num,
                         int numOfLines)
{
    int n = num, num2 = 0,
        limit = 1;
 
    // No. of rows to
    // be printed
    for (int i = 0;
             i < numOfLines; i++)
    {
         
        // No. of elements to be
        // printed in each row
        for (int j = 0; j < limit; j++)
        {
            if (j == 0)
                num2 = num;
 
            // Print the element
            Console.Write(num2++ + " ");
        }
         
        num *= 2;
        limit = num / n;
        Console.Write("\n");
    }
}
 
// Driver code
public static void Main()
{
    int num = 3;
    int numOfLines = 3;
 
    printPattern(num, numOfLines);
}
}
 
// This code is contributed by Smitha


PHP


Javascript


输出:
3 
6 7 
12 13 14 15