Open In App

Program to print numeric pattern | Set - 2

Last Updated : 23 Dec, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a number as 'num', and a number of lines as 'num_of_lines' where 'num' implies the starting number from which the pattern has to be started and 'num_of_lines' implies the number of lines that have to be printed. Now, according to the above information, print a pattern as given below.

Examples: 

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

Observations: 

  • Elements of the first column are the multiple of the previous element in that column.
  • No. of elements in each row is twice the no. of elements in the previous row.
  • Also, to generate next element in the row, add 1 to the previous element of that row.

Approach: 
So, start a loop from 0 to num_of_lines-1, to take care of the number of rows to be printed and another loop inside the first loop, from 0 till limit-1, the limit will be initialized to 1, and its value is increased exponentially. Now inside the loop, just increase the number by 1 to print the next number of that row. 
 

C++
// C++ program to print the 
// given numeric pattern
#include <bits/stdc++.h>
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
<?php 
// PHP program to print the 
// given numeric pattern

// Function to print th epattern
function printPattern($num, $numOfLines)
{
    $n = $num;
    $limit = 1;

    // No. of rows to be printed
    for ($i = 0; $i < $numOfLines; $i++) 
    {
        
        // No. of elements to be
        // printed in each row
        for ($j = 0; $j < $limit; $j++) 
        {
            if ($j == 0)
                $num2 = $num;

            // Print the element
            echo $num2++ . " ";
        }
        $num *= 2;
        $limit = $num / $n;
        echo "\n";
    }
}

// Driver code
$num = 3;
$numOfLines = 3;

printPattern($num, $numOfLines);
    
// This code is contributed 
// by ChitraNayal
?>
JavaScript
<script>

  // JavaScript program to print the
 // given numeric pattern

  // Function to print th epattern
     function printPattern(num, numOfLines)
     {
        var n = num,
          num2,
          x = 1,
          limit = 1;

        // No. of rows to be printed
        for (var i = 0; i < numOfLines; i++) 
        {
          // No. of elements to be 
          // printed in each row
          for (var j = 0; j < limit; j++) {
            if (j == 0) num2 = num;

            // Print the element
            document.write(num2++ + "&nbsp;&nbsp;");
          }
          num *= 2;
          limit = num / n;
          document.write("<br>");
        }
      }

      // Drivers code
      var num = 3;
      var numOfLines = 3;

      printPattern(num, numOfLines);
      
 </script>

Output: 
3 
6 7 
12 13 14 15

 

Time Complexity: O(pow(2,n)), Here n is the number of lines.
Auxiliary Space: O(1), As constant extra space is used.


Next Article

Similar Reads