Program to Diagonal and sides of a Rhombus Star Pattern
In this article we are going to see how to print Diagonal and sides of a Rhombus star pattern.
Example-1 For size value =9 * *** * * * * * * ********* * * * * * * *** *
Example-2 For size value = 7 * *** * * * ******* * * * *** *
Now, let’s see the actual program to print 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 the size and store it in an integer variable
size
. - Take first for loop to print all rows.
- Take second/inner for loop to print column values.
- Use an if-else for the vertical and horizontal lines
- Then go on printing the star symbols according to the iteration and if else condition.
JAVA Code:
Method-1 : Static Star Character
import java.util.Scanner; class pattern { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Size : "); //Taking size as input from user int r, c, size=scan.nextInt(); int uh = (size - 1) / 2; //Logical equation to print the upper half int lh = (size * 3) / 2 - 1; //Logical equation to print the lower half for (r = 0; r < size; r++) { //Outer Loop for (c = 0; c < size; c++) //Inner Loop { if (r + c == uh || r - c == uh || c - r == uh || r + c == lh || r == uh || c == uh) //Condition to print star System.out.print("*"); else System.out.print(" "); } //Prints a new line System.out.println(); } }
Output- Size : 7 * *** * * * ******* * * * *** *
Method-2 : User Input Character
import java.util.Scanner; class Main { public static void main(String[] args) { int r, c, size; char sym; Scanner scan = new Scanner(System.in); System.out.print("Size : "); //Taking size as input from user size=scan.nextInt(); System.out.print("Character : "); //Taking size as input from user sym=scan.next().charAt(0); int uh = (size - 1) / 2; //Logical equation to print the upper half int lh = (size * 3) / 2 - 1; //Logical equation to print the lower half for (r = 0; r < size; r++) { //Outer Loop for (c = 0; c < size; c++) //Inner Loop { if (r + c == uh || r - c == uh || c - r == uh || r + c == lh || r == uh || c == uh) //Condition to print star System.out.print(sym); else System.out.print(" "); } //Prints a new line System.out.println(); } } }
Output- Size : 7 Character : * * *** * * * ******* * * * *** *
C Code:
#include <stdio.h> int main(int argc, char const *argv[]) { printf("Size : "); //Taking size as input from user int size, r, c; scanf("%d", &size); int uh = (size - 1) / 2; //Logical equation to print the upper half int lh = (size * 3) / 2 - 1; //Logical equation to print the lower half for (r = 0; r < size; r++) { //Outer Loop for (c = 0; c < size; c++) //Inner Loop { if (r + c == uh || r - c == uh || c - r == uh || r + c == lh || r == uh || c == uh) //Condition to print star//Condition to print star printf("*"); else printf(" "); } //Prints a new line printf("\n"); } return 0; }
Output- Size : 7 * *** * * * ******* * * * *** *
C++ Code:
#include <iostream> using namespace std; int main(int argc, char const *argv[]) { cout << "Size : "; //Taking size as input from user int size, r, c; cin >> size; int uh = (size - 1) / 2; //Logical equation to print the upper half int lh = (size * 3) / 2 - 1; //Logical equation to print the lower half for (r = 0; r < size; r++) { //Outer Loop for (c = 0; c < size; c++) //Inner Loop { if (r + c == uh || r - c == uh || c - r == uh || r + c == lh || r == uh || c == uh) //Condition to print star cout << "*"; else cout << " "; } //To print a new line cout << endl; } return 0; }
Output- Size : 7 * *** * * * ******* * * * *** *
Related Java Star Pattern Programs: