Program to Print Hollow Diamond Shape Star Pattern
In this article we are going to see how to print the Hollow Diamond shape Star program.
Example-1
When row value=4
*
* *
* *
* *
* *
* *
*
Example-2:
When row value=5
*
* *
* *
* *
* *
* *
* *
* *
*
Now, let’s see program printing it.
Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.
Approach:
- Enter total row and store it in an integer variable
row. - Take first outer for loop to print all rows.
- Take first inner for loop to print column values i.e., first inner for loop will print all the spaces in the column.
- Print stars.
- Take second for loop to print column values i.e., second inner for loop will print all the spaces & new line in the column.
- Take second outer for loop to print all rows.
- Take first inner for loop to print column values i.e., first inner for loop will print all the spaces in the column.
- Print stars.
- Take second for loop to print column values i.e., second inner for loop will print all the spaces & new line in the column.
JAVA Code:
Method-1 : Static Star Character
import java.util.*;
public class Main
{
public static void main(String args[])
{
// taking variable for loop iteration and row .
int row,r,c,d;
//creating object
Scanner s = new Scanner(System.in);
// entering the number of row
System.out.print("Enter rows : ");
row = s.nextInt();
//for loop for rows
for(r=1; r<=row ; r++)
{
//printing space
for(c=row ; c>r; c--)
System.out.print(" ");
//printing star
System.out.print("*");
for(c=1; c<(r-1)*2; c++)
System.out.print(" ");
//moving to next line
if(r==1)
System.out.print("\n");
else
System.out.print("*\n");
}
//print lower triangle
for(r=row -1; r>=1; r--)
{
//printing space
for(c=row ;c>r; c--)
System.out.print(" ");
//printing star
System.out.print("*");
for(c=1; c<(r-1)*2; c++)
System.out.print(" ");
//moving to next line
if(r==1)
System.out.print("\n");
else
System.out.print("*\n");
}
}
}
Output: Enter rows : 4 * * * * * * * * * * * *
Method-2 : User Input Character
import java.util.*;
public class Main
{
public static void main(String args[])
{
// taking variable for loop iteration and row .
int row,r,c,d;
//creating object
Scanner s = new Scanner(System.in);
// entering the number of row
System.out.print("Enter rows : ");
row = s.nextInt();
// Enter any random Character
System.out.print("Enter character : ");
char sys = s.next().charAt(0);
//for loop for rows
for(r=1; r<=row ; r++)
{
//printing space
for(c=row ; c>r; c--)
System.out.print(" ");
//printing star
System.out.print(sys);
for(c=1; c<(r-1)*2; c++)
System.out.print(" ");
//moving to next line
if(r==1)
System.out.print("\n");
else
System.out.print(sys+"\n");
}
//print lower triangle
for(r=row -1; r>=1; r--)
{
//printing space
for(c=row ;c>r; c--)
System.out.print(" ");
//printing star
System.out.print(sys);
for(c=1; c<(r-1)*2; c++)
System.out.print(" ");
//moving to next line
if(r==1)
System.out.print("\n");
else
System.out.print(sys+"\n");
}
}
}
Output:
Enter rows : 4
Enter character : #
#
# #
# #
# #
# #
# #
#
C Code:
#include <stdio.h>
int main() {
int r, row, c ,d;
printf("Enter rows: ");
scanf("%d", &row);
for(r=1; r<=row ; r++)
{
//printing space
for(c=row ; c>r; c--)
printf(" ");
//printing star
printf("*");
for(c=1; c<(r-1)*2; c++)
printf(" ");
//moving to next line
if(r==1)
printf("\n");
else
printf("*\n");
}
//print lower triangle
for(r=row -1; r>=1; r--)
{
//printing space
for(c=row ;c>r; c--)
printf(" ");
//printing star
printf("*");
for(c=1; c<(r-1)*2; c++)
printf(" ");
//moving to next line
if(r==1)
printf("\n");
else
printf("*\n");
}
return 0;
}
Output: Enter rows: 4 * * * * * * * * * * * *
C++ Code:
#include <iostream>
using namespace std;
int main()
{
int row, r , c ,d ;
cout << "Enter rows: ";
cin >> row;
for(r=1; r<=row ; r++)
{
//printing space
for(c=row ; c>r; c--)
cout <<" " ;
//printing star
cout <<"*" ;
for(c=1; c<(r-1)*2; c++)
cout <<" " ;
//moving to next line
if(r==1)
cout <<"\n" ;
else
cout <<"*\n" ;
}
//print lower triangle
for(r=row -1; r>=1; r--)
{
//printing space
for(c=row ;c>r; c--)
cout << " " ;
//printing star
cout << "*" ;
for(c=1; c<(r-1)*2; c++)
cout << " " ;
//moving to next line
if(r==1)
cout << "\n" ;
else
cout << "*\n" ;
}
return 0;
}
Output: Enter rows: 4 * * * * * * * * * * * *
Related Java Star Pattern Programs: