Program to print half diamond Number-Star pattern
Given a number N which represents the number of rows. The task is to print a half diamond Number-Star pattern as shown in the below examples.
Note: N is always an even number.
Examples:
Input: N = 4
Output: 2*2
1
1
2*2
Input: N = 6
Output: 3*3*3
2*2
1
1
2*2
3*3*3
On carefully observing the above pattern, it can be broken down into two different triangles:
- An upside down right angled triangle.
- A normal right angled triangle.
For the first triangle pattern:
- Run two loops, first will take care of the number of rows and other will take care of the ‘*’ and numbers being printed.
- Inside the loops check whether the iterations of inner loop has reached to the given number of rows or not.
- If yes, print only number, otherwise print ‘*’ and number both.
The same approach will be followed for the second triangle pattern with a slight change that here the condition will be checked where the inner iteration is equal to 0 or not.
Below is the implementation of above approach:
C++
#include <iostream>
using namespace std;
void printPattern( int r)
{
r = r / 2;
int count = r;
for ( int i = 0; i < r; i++) {
for ( int j = r; j > i; j--) {
if (j != r) {
cout << "*" << count;
}
else {
cout << count;
}
}
count--;
cout << endl;
}
count++;
for ( int i = 0; i < r; i++) {
for ( int j = 0; j <= i; j++) {
if (j != 0) {
cout << "*" << count;
}
else {
cout << count;
}
}
count++;
cout << endl;
}
}
int main()
{
int n;
n = 6;
printPattern(n);
return 0;
}
|
Java
class GFG
{
static void printPattern( int r)
{
r = r / 2 ;
int count = r;
for ( int i = 0 ; i < r; i++)
{
for ( int j = r; j > i; j--)
{
if (j != r)
{
System.out.print( "*" + count);
}
else
{
System.out.print(count);
}
}
count--;
System.out.print( "\n" );
}
count++;
for ( int i = 0 ; i < r; i++)
{
for ( int j = 0 ; j <= i; j++)
{
if (j != 0 )
{
System.out.print( "*" + count);
}
else
{
System.out.print(count);
}
}
count++;
System.out.print( "\n" );
}
}
public static void main(String [] args)
{
int n;
n = 6 ;
printPattern(n);
}
}
|
Python 3
def printPattern(r) :
r = r / / 2
count = r
for i in range (r) :
for j in range (r, i, - 1 ) :
if j ! = r :
print ( "*" + str (count),
end = "")
else :
print (count, end = "")
count - = 1
print ()
count + = 1
for i in range (r) :
for j in range (i + 1 ):
if j ! = 0 :
print ( "*" + str (count),
end = "")
else :
print (count, end = "")
count + = 1
print ()
if __name__ = = "__main__" :
n = 6
printPattern(n)
|
C#
using System;
public class GFG
{
static void printPattern( int r)
{
r = r / 2;
int count = r;
for ( int i = 0; i < r; i++) {
for ( int j = r; j > i; j--) {
if (j != r) {
Console.Write( "*" + count);
}
else {
Console.Write( count);
}
}
count--;
Console.WriteLine();
}
count++;
for ( int i = 0; i < r; i++) {
for ( int j = 0; j <= i; j++) {
if (j != 0) {
Console.Write( "*" + count);
}
else {
Console.Write( count);
}
}
count++;
Console.WriteLine();
}
}
public static void Main()
{
int n;
n = 6;
printPattern(n);
}
}
|
PHP
<?php
function printPattern( $r )
{
$r = $r / 2;
$count = $r ;
for ( $i = 0; $i < $r ; $i ++)
{
for ( $j = $r ; $j > $i ; $j --)
{
if ( $j != $r )
{
echo "*" . $count ;
}
else
{
echo $count ;
}
}
$count --;
echo "\n" ;
}
$count ++;
for ( $i = 0; $i < $r ; $i ++)
{
for ( $j = 0; $j <= $i ; $j ++)
{
if ( $j != 0)
{
echo "*" . $count ;
}
else
{
echo $count ;
}
}
$count ++;
echo "\n" ;
}
}
$n = 6;
printPattern( $n );
?>
|
Javascript
<script>
function printPattern(r) {
r = r / 2;
var count = parseInt(r);
for ( var i = 0; i < r; i++) {
for ( var j = r; j > i; j--) {
if (j != r) {
document.write( "*" + count);
} else {
document.write(count);
}
}
count--;
document.write( "<br>" );
}
count++;
for ( var i = 0; i < r; i++) {
for ( var j = 0; j <= i; j++) {
if (j != 0) {
document.write( "*" + count);
} else {
document.write(count);
}
}
count++;
document.write( "<br>" );
}
}
var n;
n = 6;
printPattern(n);
</script>
|
Output:
3*3*3
2*2
1
1
2*2
3*3*3
Time Complexity: O(r2), where r represents the given input.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Last Updated :
06 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...