Program to Print Inverted Heart Star Pattern
In this article we are going to see how to print Inverted Heart star program.
Approach:
- Take values for loop iteration , upper part of inverted heart , base part of inverted heart.
- Take first for loop to print the row value of upper part of inverted heart.
- Calculate the space for the upper part .
- Using space value take a first inner loop and print the number of spaces for upper part .
- Take second inner for loop and print the stars for column value .
- Again take a third for loop and print the space for column value .
- Take number of spaces for base part as
seand number of space between base part assband assign them with 0. - Take another for loop i.e., second for loop to print the base part of inverted heart .
- Take first inner for loop to print the space . ( using
se) - Take second inner for loop to print the stars .
- Take third for loop to print the space . ( using
sb) - Take fourth for loop to print the stars.
- Tale fifth for loop to print the spaces . ( using
se) - For each iteration increase the
sevalue to 1 andsbvalue to 2 .
- Take first inner for loop to print the space . ( using
- Then go on printing the star symbol according to loop.
Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.
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 size = 13;
// size of upper part of Inverted Heart
int uh = 7;
// size of base part of Inverted Heart
int bs = 4;
// number of characters to be printed in a row
int n = 1;
// loop for printing upper part
for (int r = 0; r < uh ; r++)
{
// white spaces to be printed before and after triangle
int spaces = (size - n) / 2;
//for loop to print spaces
for (int c = 0; c < spaces; c++)
System.out.print(" ");
// for loop to print stars
for (int c = 0; c < n; c++)
System.out.print("*");
// for loop to print spaces
for (int c = 0; c < spaces; c++)
System.out.print(" ");
//taking to the next line
System.out.print("\n");
n = n + 2;
}
// declaring number of spaces to be printed in base part
int se = 0;
// declaring number of spaces to be printed between two part of the base
int sb = 0;
// loop for printing base part
for (int r = 0; r < bs; r++)
{
n = (size - (2 * (se)) - sb) / 2;
// loop for printing space before base part-1
for (int c = 0; c < se; c++)
System.out.print(" ");
// loop for printing base part-1
for (int c = 0; c < n; c++)
System.out.print("*");
// loop for printing spaces between two base
for (int c = 0; c < sb; c++)
System.out.print(" ");
// For loop to print base part-2
for (int c = 0; c < n; c++)
System.out.print("*");
// loop for printing space before base part-2
for (int c = 0; c < se; c++)
System.out.print(" ");
// taking to new line
System.out.print("\n");
se++;
sb = sb + 2;
}
}
}
Output :
*
***
*****
*******
*********
***********
*************
************
**** ****
** **
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 size = 13;
// size of upper part of Inverted Heart
int uh = 7;
// size of base part of Inverted Heart
int bs = 4;
// number of characters to be printed in a row
int n = 1;
Scanner sc=new Scanner(System.in);
// Entering any random character
System.out.print("Enter any random chracter = ");
char in=sc.next().charAt(0);
// loop for printing upper part
for (int r = 0; r < uh ; r++)
{
// white spaces to be printed before and after triangle
int spaces = (size - n) / 2;
//for loop to print spaces
for (int c = 0; c < spaces; c++)
System.out.print(" ");
// for loop to print stars
for (int c = 0; c < n; c++)
System.out.print(in);
// for loop to print spaces
for (int c = 0; c < spaces; c++)
System.out.print(" ");
//taking to the next line
System.out.print("\n");
n = n + 2;
}
// declaring number of spaces to be printed in base part
int se = 0;
// declaring number of spaces to be printed between two part of the base
int sb = 0;
// loop for printing base part
for (int r = 0; r < bs; r++)
{
n = (size - (2 * (se)) - sb) / 2;
// loop for printing space before base part-1
for (int c = 0; c < se; c++)
System.out.print(" ");
// loop for printing base part-1
for (int c = 0; c < n; c++)
System.out.print(in);
// loop for printing spaces between two base
for (int c = 0; c < sb; c++)
System.out.print(" ");
// For loop to print base part-2
for (int c = 0; c < n; c++)
System.out.print(in);
// loop for printing space before base part-2
for (int c = 0; c < se; c++)
System.out.print(" ");
// taking to new line
System.out.print("\n");
se++;
sb = sb + 2;
}
}
}
Output : Enter any random character : 0 0 000 00000 0000000 000000000 0000000000 000000000000 000000000000 0000 0000 00 00
C Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c ,r, size = 13 ,uh = 7, bs = 3 ,n = 1, spaces, se = 0, sb = 0;
for ( r = 0; r < uh ; r++)
{
spaces = (size - n) / 2;
for ( c = 0; c < spaces; c++)
printf(" ");
for ( c = 0; c < n; c++)
printf("*");
for ( c = 0; c < spaces; c++)
printf(" ");
printf("\n");
n = n + 2;
}
for ( r = 0; r < bs; r++)
{
n = (size - (2 * (se)) - sb) / 2;
for ( c = 0; c < se; c++)
printf(" ");
for ( c = 0; c < n; c++)
printf("*");
for ( c = 0; c < sb; c++)
printf(" ");
for ( c = 0; c < n; c++)
printf("*");
for ( c = 0; c < se; c++)
printf(" ");
printf("\n");
se++;
sb = sb + 2;
}
return 0;
}
Output : * *** ***** ******* ********* *********** ************* ************ **** **** ** **
C++ Code:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int c ,r, size = 13 ,uh = 7, bs = 3 ,n = 1, spaces, se = 0, sb = 0;
for ( r = 0; r < uh ; r++)
{
spaces = (size - n) / 2;
for ( c = 0; c < spaces; c++)
cout << " ";
for ( c = 0; c < n; c++)
cout << "*";
for ( c = 0; c < spaces; c++)
cout << " ";
cout << "\n";
n = n + 2;
}
for ( r = 0; r < bs; r++)
{
n = (size - (2 * (se)) - sb) / 2;
for ( c = 0; c < se; c++)
cout << " ";
for ( c = 0; c < n; c++)
cout << "*";
for ( c = 0; c < sb; c++)
cout << " ";
for ( c = 0; c < n; c++)
cout << "*";
for ( c = 0; c < se; c++)
cout << " ";
cout << "\n";
se++;
sb = sb + 2;
}
return 0;
}
Output :
*
***
*****
*******
*********
***********
*************
************
**** ****
** **
Related Java Star Pattern Programs: