Program to Print Heart Star Pattern
In this article we are going to see how to print the Heart star pattern.
Example-1
When row value is 7
*** ***
***** *****
**************
*************
***********
*********
*******
*****
***
*
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
Approach:
- Enter total row and store it in an integer variable say
row. - Take a inner loop to print the column values.
- Take a nested for loop to print left semi-circle at the beginning.
- Take a nested for loop to print right semi-circle beginning.
- Take another nested loop to print a inverted pyramid below the semi-circles.
JAVA Code:
Method-1 : Static Star Character
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int r,c, row;
Scanner sc= new Scanner(System.in);
System.out.print("Enter no of rows = ");
row=sc.nextInt();
// loop to print 2 semicircles at the top
for(r = row/2; r <= row; r+=2)
{
// print spaces
for(c = 1; c < row-r; c+=2)
{
System.out.print(" ");
}
// loop to print left semi-circle
for(c = 1; c <= r; c++)
{
System.out.print("*");
}
// print spaces
for(c = 1; c <= row-r; c++)
{
System.out.print(" ");
}
// loop to print right semi-circle
for(c = 1; c <= r; c++)
{
System.out.print("*");
}
// move to next line/row
System.out.println("");
}
// loop to print the inverted pyramid
for(r = row; r >= 1; r--)
{
// adds spaces in each row
for(c = r; c < row; c++)
{
System.out.print(" ");
}
// keep on printing star from c=1 till (r*2)-1
for(c = 1; c <= (r*2)-1; c++)
{
System.out.print("*");
}
// move to next line/row
System.out.println("");
}
}
}
Output:
Enter no of rows = 10
***** *****
******* *******
********* *********
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
Method-2 : User Input Character
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int r,c, row;
Scanner sc= new Scanner(System.in);
System.out.print("Enter no of rows = ");
row=sc.nextInt();
// Enter any random Character
System.out.print("Enter any character = ");
char s=sc.next().charAt(0);
// loop to print 2 semicircles at the top
for(r = row/2; r <= row; r+=2)
{
// print spaces
for(c = 1; c < row-r; c+=2)
{
System.out.print(" ");
}
// loop to print left semi-circle
for(c = 1; c <= r; c++)
{
System.out.print(s);
}
// print spaces
for(c = 1; c <= row-r; c++)
{
System.out.print(" ");
}
// loop to print right semi-circle
for(c = 1; c <= r; c++)
{
System.out.print(s);
}
// move to next line/row
System.out.println("");
}
// loop to print the inverted pyramid
for(r = row; r >= 1; r--)
{
// adds spaces in each row
for(c = r; c < row; c++)
{
System.out.print(" ");
}
// keep on printing star from c=1 till (r*2)-1
for(c = 1; c <= (r*2)-1; c++)
{
System.out.print(s);
}
// move to next line/row
System.out.println("");
}
}
}
Output: Enter no of rows = 10 Enter any character = * ***** ***** ******* ******* ********* ********* ******************* ***************** *************** ************* *********** ********* ******* ***** *** *
C Code:
#include<stdio.h>
int main()
{
int r,c, row;
printf("Enter no of rows = ");
scanf("%d", &row);
for(r = row/2; r <= row; r+=2){
for(c = 1; c < row-r; c+=2) {
printf(" ");
}
for(c = 1; c <= r; c++){
printf("*");
}
for(c = 1; c <= row-r; c++){
printf(" ");
}
for(c = 1; c <= r; c++){
printf("*");
}
printf("\n");
}
for(r = row; r >= 1; r--){
for(c = r; c < row; c++){
printf(" ");
}
for(c = 1; c <= (r*2)-1; c++){
printf("*");
}
printf("\n");
}
return 0;
}
Output: Enter no of rows = 10 ***** ***** ******* ******* ********* ********* ******************* ***************** *************** ************* *********** ********* ******* ***** *** *
C++ Code:
#include <iostream>
using namespace std;
int main()
{
int r,c,row;
cout<<"Enter no of rows = ";
cin>>row;
for(r = row/2; r <= row; r+=2){
for(c = 1; c < row-r; c+=2) {
cout<<" ";
}
for(c = 1; c <= r; c++){
cout<<"*";
}
for(c = 1; c <= row-r; c++){
cout<<" ";
}
for(c = 1; c <= r; c++){
cout<<"*";
}
cout<<"\n";
}
for(r = row; r >= 1; r--){
for(c = r; c < row; c++){
cout<<" ";
}
for(c = 1; c <= (r*2)-1; c++){
cout<<"*";
}
cout<<"\n";
}
return 0;
}
Output: Enter no of rows = 10 ***** ***** ******* ******* ********* ********* ******************* ***************** *************** ************* *********** ********* ******* ***** *** *
Also Read: Java Program to Print Inverted Heart Star Pattern
Answer these:
- Draw a heart using graphics class in java?
- How to print heart emoji in java?
- Hollow heart pattern in java?
- Pattern program in java?
- Java program for love propose?
- C program to print heart pattern with name?
- Write a program to print a figure in the shape of a heart by the sign o?
- Java program to print heart pattern with name?
- Java program to print a star pattern?
- Java program to print star pattern in triangle?
- Java program to print star pattern using while loop?
- Write a program to print star pattern in java?
- Java program to print heart shape?
- Heart shape code?
Related Java Star Pattern Programs: