Program to print numeric pattern | Set – 2
Given a number as ‘num’, and 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 the twice of the no. of elements in the previous row.
- Also, to generate next element in the row, add 1 to 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 coode 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 ?> |
3 6 7 12 13 14 15
Recommended Posts:
- Program to print numeric pattern
- Program to print the pattern ‘D’
- Program to print the pattern 'G'
- Program to print the given H Pattern
- Program to print the given Z Pattern
- Program to print the pattern "GFG"
- Program to print pattern
- Program to print the arrow pattern
- Program to print Kite Pattern
- Program to print circle pattern
- Program to print Step Pattern
- Program to print V and inverted-V pattern
- Program to print Swastika Pattern
- Program to print Crown Pattern
- Program to print Hut Star pattern
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



