Program to Print Heart Character Pattern
In the previous article, we have discussed Java Program to Print Ladder Character Pattern
In this article we are going to see how to print the Heart character pattern.
Output : When no of rows 8 ABCD ABCD ABCDEF ABCDEF ABCDEFGHABCDEFGH ABCDEFGHIJKLMNO ABCDEFGHIJKLM ABCDEFGHIJK ABCDEFGHI ABCDEFG ABCDE ABC A
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 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 to Print Heart Character Pattern
import java.util.Scanner; public class Main { public static void main(String[] args) { //variables declared to iterate rows and columns // variable 'row' represents number of rows. int r,c, row; //Starting ASCII Value int ascii=64; //Scanner class object created 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((char)(c+ascii)); } // 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((char)(c+ascii)); } // 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((char)(c+ascii)); } // move to next line/row System.out.println(""); } } }
Output:
Enter no of rows : 8 ABCD ABCD ABCDEF ABCDEF ABCDEFGHABCDEFGH ABCDEFGHIJKLMNO ABCDEFGHIJKLM ABCDEFGHIJK ABCDEFGHI ABCDEFG ABCDE ABC A
Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.
Related Java Character Pattern Programs: