Program to print numeric pattern | Set - 2
Last Updated :
23 Dec, 2022
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++ + " ");
}
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.
Similar Reads
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
TCP/IP Model The TCP/IP model is a framework that is used to model the communication in a network. It is mainly a collection of network protocols and organization of these protocols in different layers for modeling the network.It has four layers, Application, Transport, Network/Internet and Network Access.While
7 min read
Types of Network Topology Network topology refers to the arrangement of different elements like nodes, links, or devices in a computer network. Common types of network topology include bus, star, ring, mesh, and tree topologies, each with its advantages and disadvantages. In this article, we will discuss different types of n
12 min read
Basics of Computer Networking A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br
14 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Unified Modeling Language (UML) Diagrams Unified Modeling Language (UML) is a general-purpose modeling language. The main aim of UML is to define a standard way to visualize the way a system has been designed. It is quite similar to blueprints used in other fields of engineering. UML is not a programming language, it is rather a visual lan
14 min read
Second Largest Element in an Array Given an array of positive integers arr[] of size n, the task is to find second largest distinct element in the array.Note: If the second largest element does not exist, return -1. Examples:Input: arr[] = [12, 35, 1, 10, 34, 1]Output: 34Explanation: The largest element of the array is 35 and the sec
14 min read
Python Lists In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe s
6 min read
Introduction to Java Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android
4 min read
f-strings in Python Python offers a powerful feature called f-strings (formatted string literals) to simplify string formatting and interpolation. f-strings is introduced in Python 3.6 it provides a concise and intuitive way to embed expressions and variables directly into strings. The idea behind f-strings is to make
5 min read