Posted on by Kalkicode
Code Pattern

Print rhombus pattern

In this article, we will discuss how to print a rhombus pattern using C programming language. We will start with an introduction to the problem, explain the problem statement, provide a suitable example, describe the algorithm and pseudocode, and finally, explain the resultant output with the time complexity of the code.

Introduction

The rhombus pattern is a geometric pattern consisting of asterisks (*) arranged in the shape of a rhombus. The size of the rhombus determines the number of rows and columns it occupies. The goal is to write a program that takes the size of the rhombus as input and prints the corresponding rhombus pattern using asterisks.

Problem Statement

The problem can be defined as follows:

Write a C program to print a rhombus pattern of a given size. The program should take an integer input representing the size of the rhombus and display the rhombus pattern using asterisks (*) and spaces.

Example

Let's consider an example to understand the problem better. Suppose we want to print a rhombus pattern of size 4. The expected output would be:

   ****
  ****
 ****
****

The asterisks form a rhombus shape, and each line is indented from the left by an increasing number of spaces.

Algorithm and Pseudocode

To solve this problem, we can follow the following algorithm:

  1. Define a function space to print the required number of spaces.
  2. Define a function print_symbol to print the required number of asterisks.
  3. Define a function rhombus_pattern to display the rhombus pattern of the given size.
  4. In the rhombus_pattern function, iterate from i = 1 to size (inclusive).
  5. Inside the loop, call the space function with the argument size - i to print the required spaces.
  6. Call the print_symbol function with the argument size to print the required asterisks.
  7. Print a newline character to move to the next line.
  8. Call the rhombus_pattern function from the main function with different test cases to demonstrate the functionality.

Here's the pseudocode for the algorithm:

function space(size):
    for counter from 0 to size:
        print a space

function print_symbol(size):
    for counter from 0 to size:
        print an asterisk

function rhombus_pattern(size):
    print "Size: " + size
    for i from 1 to size:
        call space(size - i)
        call print_symbol(size)
        print a newline

main():
    call rhombus_pattern(4)
    call rhombus_pattern(5)
    call rhombus_pattern(7)

Code Solution

//C Program 
//Print rhombus pattern
#include <stdio.h>

#include <stdlib.h>

/*Include Space of given size*/
void space(int size) {

  int counter = 0;

  for (counter = 0; counter < size; counter++) {

    //Add space
    printf(" ");
  }
}
/*Include Symbol*/
void print_symbol(int size) {

  int counter = 0;

  for (counter = 0; counter < size; counter++) {

    printf("*");
    
  }
}

//Display rhombus pattern of given size
void rhombus_pattern(int size) {

  printf("\n Size : %d \n\n", size);
  int i = 0;

  for (i = 1; i <= size; i++) {
    space(size-i);
    print_symbol(size);
    printf("\n");
  }
}


int main() {
  //Test Cases
  rhombus_pattern(4);
  rhombus_pattern(5);
  rhombus_pattern(7);
  return 0;
}

Output

 Size : 4

   ****
  ****
 ****
****

 Size : 5

    *****
   *****
  *****
 *****
*****

 Size : 7

      *******
     *******
    *******
   *******
  *******
 *******
*******
/*
  C++ Program
  Print rhombus pattern
*/
#include<iostream>

using namespace std;
class MyPattern {
	public:

    /*Include Space of given size*/
    void space(int size) {
      int counter = 0;
      for (counter = 0; counter < size; counter++) {
        //Add space

        cout << " ";
      }
    }
	/*Include Symbol*/
	void print_symbol(int size) {
		int counter = 0;
		for (counter = 0; counter < size; counter++) {
			cout << "*";
		}
	}
	//Display rhombus pattern of given size
	void rhombus_pattern(int size) {
		cout << "\n Size : " << size << " \n\n";
		int i = 0;
		for (i = 1; i <= size; i++) {
			this->space(size - i);
			this->print_symbol(size);
			cout << "\n";
		}
	}
};
int main() {
	MyPattern obj = MyPattern();
	//Test Case
	obj.rhombus_pattern(4);
	obj.rhombus_pattern(5);
	obj.rhombus_pattern(7);
	return 0;
}

Output

 Size : 4

   ****
  ****
 ****
****

 Size : 5

    *****
   *****
  *****
 *****
*****

 Size : 7

      *******
     *******
    *******
   *******
  *******
 *******
*******
/*
  Java Program
  Print rhombus pattern
*/

public class MyPattern {


  /*Include Space of given size*/
  public void space(int size) {

    int counter = 0;

    for (counter = 0; counter < size; counter++) {

      //Add space
      System.out.print(" ");
    }
  }
  /*Include Symbol*/
  public void print_symbol(int size) {

    int counter = 0;

    for (counter = 0; counter < size; counter++) {

      System.out.print("*");
      
    }
  }

  //Display rhombus pattern of given size
  public void rhombus_pattern(int size) {

    System.out.print("\n Size : "+size+" \n\n");
    int i = 0;

    for (i = 1; i <= size; i++) {
      space(size-i);
      print_symbol(size);
      System.out.print("\n");
    }
  }
  public static void main(String[] args) {

    MyPattern obj = new MyPattern();
    //Test Case
    obj.rhombus_pattern(4);
    obj.rhombus_pattern(5);
    obj.rhombus_pattern(7);

  }
}

Output

 Size : 4

   ****
  ****
 ****
****

 Size : 5

    *****
   *****
  *****
 *****
*****

 Size : 7

      *******
     *******
    *******
   *******
  *******
 *******
*******
/*
  C# Program
  Print rhombus pattern
*/
using System;

public class MyPattern {
	/*Include Space of given size*/
	public void space(int size) {
		int counter = 0;
		for (counter = 0; counter < size; counter++) {
			Console.Write(" ");
		}
	}
	/*Include Symbol*/
	public void print_symbol(int size) {
		int counter = 0;
		for (counter = 0; counter < size; counter++) {
			Console.Write("*");
		}
	}
	//Display rhombus pattern of given size
	public void rhombus_pattern(int size) {
		Console.Write("\n Size : " + size + " \n\n");
		int i = 0;
		for (i = 1; i <= size; i++) {
			space(size - i);
			print_symbol(size);
			Console.Write("\n");
		}
	}
	public static void Main(String[] args) {
		MyPattern obj = new MyPattern();
		obj.rhombus_pattern(4);
		obj.rhombus_pattern(5);
		obj.rhombus_pattern(7);
	}
}

Output

 Size : 4

   ****
  ****
 ****
****

 Size : 5

    *****
   *****
  *****
 *****
*****

 Size : 7

      *******
     *******
    *******
   *******
  *******
 *******
*******
<?php
/*
  Php Program
  Print rhombus pattern
*/
class MyPattern {
	/*Include Space of given size*/
	public 	function space($size) {
		$counter = 0;
		for ($counter = 0; $counter < $size; $counter++) {
			//Add space

			echo(" ");
		}
	}
	/*Include Symbol*/
	public 	function print_symbol($size) {
		$counter = 0;
		for ($counter = 0; $counter < $size; $counter++) {
			echo("*");
		}
	}
	//Display rhombus pattern of given size
	public 	function rhombus_pattern($size) {
		echo("\n Size : ". $size ." \n\n");
		$i = 0;
		for ($i = 1; $i <= $size; $i++) {
			$this->space($size - $i);
			$this->print_symbol($size);
			echo("\n");
		}
	}
}

function main() {
	$obj = new MyPattern();
	//Test Case
	$obj->rhombus_pattern(4);
	$obj->rhombus_pattern(5);
	$obj->rhombus_pattern(7);

}
main();

Output

 Size : 4

   ****
  ****
 ****
****

 Size : 5

    *****
   *****
  *****
 *****
*****

 Size : 7

      *******
     *******
    *******
   *******
  *******
 *******
*******
/*
  Node Js Program
  Print rhombus pattern
*/
class MyPattern {
	/*Include Space of given size*/
	space(size) {
		var counter = 0;
		for (counter = 0; counter < size; counter++) {
			//Add space

			process.stdout.write(" ");
		}
	}

	/*Include Symbol*/
	print_symbol(size) {
		var counter = 0;
		for (counter = 0; counter < size; counter++) {
			process.stdout.write("*");
		}
	}

	//Display rhombus pattern of given size
	rhombus_pattern(size) {
		process.stdout.write("\n Size : " + size + " \n\n");
		var i = 0;
		for (i = 1; i <= size; i++) {
			this.space(size - i);
			this.print_symbol(size);
			process.stdout.write("\n");
		}
	}
}

function main(args) {
	var obj = new MyPattern();
	//Test Case
	obj.rhombus_pattern(4);
	obj.rhombus_pattern(5);
	obj.rhombus_pattern(7);
}

main();

Output

 Size : 4

   ****
  ****
 ****
****

 Size : 5

    *****
   *****
  *****
 *****
*****

 Size : 7

      *******
     *******
    *******
   *******
  *******
 *******
*******
#   Python 3 Program
#   Print rhombus pattern

class MyPattern :
	# Include Space of given size 
	def space(self, size) :
		counter = 0
		while (counter < size) :
			print(" ", end = "")
			counter += 1
		
	
	# Include Symbol
	def print_symbol(self, size) :
		counter = 0
		while (counter < size) :
			print("*", end = "")
			counter += 1
		
	
	# Display rhombus pattern of given size
	def rhombus_pattern(self, size) :
		print("\n Size : ", size ," \n\n", end = "")
		i = 1
		while (i <= size) :
			self.space(size - i)
			self.print_symbol(size)
			print("\n", end = "")
			i += 1
		
	

def main() :
	obj = MyPattern()
	obj.rhombus_pattern(4)
	obj.rhombus_pattern(5)
	obj.rhombus_pattern(7)


if __name__ == "__main__":
	main()

Output

 Size :  4

   ****
  ****
 ****
****

 Size :  5

    *****
   *****
  *****
 *****
*****

 Size :  7

      *******
     *******
    *******
   *******
  *******
 *******
*******
#   Ruby Program
#   Print rhombus pattern

class MyPattern 
	# Include Space of given size
	def space(size) 
		counter = 0
		while (counter < size) 
			print(" ")
			counter += 1
		end
	end
	# Include Symbol
	def print_symbol(size) 
		counter = 0
		while (counter < size) 
			print("*")
			counter += 1
		end
	end
	 # Display rhombus pattern of given size
	def rhombus_pattern(size) 
		print("\n Size  :", size ," \n\n")
		i = 1
		while (i <= size) 
			self.space(size - i)
			self.print_symbol(size)
			print("\n")
			i += 1
		end
	end
end
def main() 
	obj = MyPattern.new()
	obj.rhombus_pattern(4)
	obj.rhombus_pattern(5)
	obj.rhombus_pattern(7)
end
main()

Output

 Size  :4 

   ****
  ****
 ****
****

 Size  :5 

    *****
   *****
  *****
 *****
*****

 Size  :7 

      *******
     *******
    *******
   *******
  *******
 *******
*******
/*
  Scala Program
  Print rhombus pattern
*/
class MyPattern {
	/*Include Space of given size*/
	def space(size: Int): Unit = {
		var counter: Int = 0;
		while (counter < size) {
			print(" ");
			counter += 1;
		}
	}
	/*Include Symbol*/
	def print_symbol(size: Int): Unit = {
		var counter: Int = 0;
		while (counter < size) {
			print("*");
			counter += 1;
		}
	}
	//Display rhombus pattern of given size
	def rhombus_pattern(size: Int): Unit = {
		print("\n Size : " + size + " \n\n");
		var i: Int = 1;
		while (i <= size) {
			this.space(size - i);
			this.print_symbol(size);
			print("\n");
			i += 1;
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		val obj: MyPattern = new MyPattern();
		obj.rhombus_pattern(4);
		obj.rhombus_pattern(5);
		obj.rhombus_pattern(7);
	}
}

Output

 Size : 4

   ****
  ****
 ****
****

 Size : 5

    *****
   *****
  *****
 *****
*****

 Size : 7

      *******
     *******
    *******
   *******
  *******
 *******
*******
/*
  Swift Program
  Print rhombus pattern
*/
class MyPattern {
	/*Include Space of given size*/
	func space(_ size: Int) {
		var counter = 0;
		while (counter < size) {
			print(" ", terminator: "");
			counter += 1;
		}
	}
	/*Include Symbol*/
	func print_symbol(_ size: Int) {
		var counter = 0;
		while (counter < size) {
			print("*", terminator: "");
			counter += 1;
		}
	}
	//Display rhombus pattern of given size
	func rhombus_pattern(_ size: Int) {
		print("\n Size : ", size ," \n\n", terminator: "");
		var i = 1;
		while (i <= size) {
			self.space(size - i);
			self.print_symbol(size);
			print("\n", terminator: "");
			i += 1;
		}
	}
}
func main() {
	let obj = MyPattern();
	obj.rhombus_pattern(4);
	obj.rhombus_pattern(5);
	obj.rhombus_pattern(7);
}
main();

Output

 Size :  4

   ****
  ****
 ****
****

 Size :  5

    *****
   *****
  *****
 *****
*****

 Size :  7

      *******
     *******
    *******
   *******
  *******
 *******
*******

Resultant Output and Time Complexity

The expected output for the provided test cases would be:

Size: 4
   ****
  ****
 ****
****

Size: 5
    *****
   *****
  *****
 *****
*****

Size: 7
      *******
     *******
    *******
   *******
  *******
 *******
*******

The time complexity of the code is O(n^2), where n is the size of the rhombus. This is because we have two nested loops: one iterating from 1 to the given size, and the other iterating from 0 to the given size. As a result, the number of iterations is proportional to the square of the input size.

By following the algorithm and pseudocode, we can print the rhombus pattern of any given size using asterisks and spaces in a visually appealing manner.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment