Skip to main content

Print rhombus pattern

Here given code implementation process.

//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

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




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