Program to print the pattern 1020304017018019020 **50607014015016 ****809012013 ******10011…
Given an integer N, the task is to print the pattern below for the given value of N.
For N = 5, below is the given pattern:
Examples:
Input: N = 4
Output:
1020304017018019020
**50607014015016
****809012013
******10011
Input: N = 3
Output:
10203010011012
**4050809
****607
Approach: The idea to understand the logic behind the given pattern is stated below:
By looking closely, we see that by replacing in-between zeroes with spaces, the pattern can be seen more clearly. The pattern is further divided into three different patterns.
- Case 1: Asterisk (*) character pattern follows a sequence from 0 and adds two more asterisks in each row, where the row is equal to N.
- Case 2: In this part, the pattern is very simple to understand. i.e the number of columns and rows will be equal to N and follows a sequence like 1, 2, 3, 4, 5…
- Case 3: Follow-up or bottom-up sequence is the interesting part where the numbers are represented from bottom to top.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int sum( int n)
{
return n * (n - 1) / 2;
}
void BSpattern( int N)
{
int Val = 0, Pthree = 0,
cnt = 0, initial;
string s = "**" ;
for ( int i = 0; i < N; i++) {
cnt = 0;
if (i > 0) {
cout << s;
s += "**" ;
}
for ( int j = i; j < N; j++) {
if (i > 0) {
cnt++;
}
cout << ++Val;
cout << 0;
}
if (i == 0) {
int Sumbeforelast = sum(Val) * 2;
Pthree = Val + Sumbeforelast + 1;
initial = Pthree;
}
initial = initial - cnt;
Pthree = initial;
for ( int k = i; k < N; k++) {
cout << Pthree++;
if (k != N - 1) {
cout << 0;
}
}
cout << "\n" ;
}
}
int main()
{
int N = 5;
BSpattern(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int sum( int n)
{
return n * (n - 1 ) / 2 ;
}
static void BSpattern( int N)
{
int Val = 0 , Pthree = 0 ,
cnt = 0 , initial = - 1 ;
String s = "**" ;
for ( int i = 0 ; i < N; i++)
{
cnt = 0 ;
if (i > 0 )
{
System.out.print(s);
s += "**" ;
}
for ( int j = i; j < N; j++)
{
if (i > 0 )
{
cnt++;
}
System.out.print(++Val);
System.out.print( "0" );
}
if (i == 0 )
{
int Sumbeforelast = sum(Val) * 2 ;
Pthree = Val + Sumbeforelast + 1 ;
initial = Pthree;
}
initial = initial - cnt;
Pthree = initial;
for ( int k = i; k < N; k++)
{
System.out.print(Pthree++);
if (k != N - 1 )
{
System.out.print( "0" );
}
}
System.out.println();
}
}
public static void main(String[] args)
{
int N = 5 ;
BSpattern(N);
}
}
|
Python3
def sum (n):
return n * (n - 1 ) / / 2
def BSpattern(N):
Val = 0
Pthree = 0 ,
cnt = 0
initial = - 1
s = "**"
for i in range (N):
cnt = 0
if (i > 0 ):
print (s, end = "")
s + = "**"
for j in range (i, N):
if (i > 0 ):
cnt + = 1
Val + = 1
print (Val, end = "")
print ( 0 , end = "")
if (i = = 0 ):
Sumbeforelast = sum (Val) * 2
Pthree = Val + Sumbeforelast + 1
initial = Pthree
initial = initial - cnt
Pthree = initial
for k in range (i, N):
print (Pthree, end = "")
Pthree + = 1
if (k ! = N - 1 ):
print ( 0 , end = "")
print ()
N = 5
BSpattern(N)
|
C#
using System;
class GFG{
static int sum( int n)
{
return n * (n - 1) / 2;
}
static void BSpattern( int N)
{
int Val = 0, Pthree = 0,
cnt = 0, initial = -1;
String s = "**" ;
for ( int i = 0; i < N; i++)
{
cnt = 0;
if (i > 0)
{
Console.Write(s);
s += "**" ;
}
for ( int j = i; j < N; j++)
{
if (i > 0)
{
cnt++;
}
Console.Write(++Val);
Console.Write( "0" );
}
if (i == 0)
{
int Sumbeforelast = sum(Val) * 2;
Pthree = Val + Sumbeforelast + 1;
initial = Pthree;
}
initial = initial - cnt;
Pthree = initial;
for ( int k = i; k < N; k++)
{
Console.Write(Pthree++);
if (k != N - 1)
{
Console.Write( "0" );
}
}
Console.WriteLine();
}
}
public static void Main(String[] args)
{
int N = 5;
BSpattern(N);
}
}
|
Javascript
<script>
function sum( n)
{
return n * parseInt((n - 1) / 2);
}
function BSpattern( N)
{
var Val = 0, Pthree = 0,
cnt = 0, initial;
var s = "**" ;
for ( var i = 0; i < N; i++) {
cnt = 0;
if (i > 0) {
document.write( s);
s += "**" ;
}
for ( var j = i; j < N; j++) {
if (i > 0) {
cnt++;
}
document.write( ++Val);
document.write( 0);
}
if (i == 0) {
var Sumbeforelast = sum(Val) * 2;
Pthree = Val + Sumbeforelast + 1;
initial = Pthree;
}
initial = initial - cnt;
Pthree = initial;
for ( var k = i; k < N; k++) {
document.write(Pthree++);
if (k != N - 1) {
document.write( 0);
}
}
document.write( "<br>" );
}
}
var N = 5;
BSpattern(N);
</script>
|
Output:
102030405026027028029030
**6070809022023024025
****10011012019020021
******13014017018
********15016
Time Complexity: O(N2)
Auxiliary Space: O(1)
Last Updated :
21 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...