In this article we are going to see how to print the downward arrow mark symbol star pattern.
Example-1 When size value=7 * * * * * * * * * *** *
Example-2 When size value=5 * * * * * *** *
Now, let’s see the actual program to print it.
Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.
Approach:
- Enter size of the pattern and store it in an integer variable size.
- Take one outer for loop to iterate the rows.
- Take one inner for loops, to iterate the columns.
- After each iteration print a new line.
JAVA Code:
import java.util.Scanner; class pattern { public static void main(String[] args) { int size, r, c; //Taking size as input from user System.out.print("Size : "); Scanner scan = new Scanner(System.in); size = scan.nextInt(); //Taking middle of the pattern in negative int mid = -size / 2 + 1; //Outer Loop for (r = 1; r <= size; r++) { //Inner loop for (c = 1; c <= size; c++) { if (c == size / 2 + 1 || c == mid || c == size - mid + 1) System.out.print("*"); else System.out.print(" "); } //Prints a newline System.out.println(); //Incrementing the mid value mid++; } } }
Output: Size : 5 * * * * * *** *
C Code:
#include <stdio.h> int main() { int size, r, c; //Taking size as input from user printf("Size : "); scanf("%d", &size); //Taking middle of the pattern in negative int mid = -size / 2 + 1; //Outer Loop for (r = 1; r <= size; r++) { //Inner loop for (c = 1; c <= size; c++) { if (c == size / 2 + 1 || c == mid || c == size - mid + 1) printf("*"); else printf(" "); } //Prints a newline printf("\n"); //incrementing the mid value mid++; } return 0; }
Output Size : 5 * * * * * *** *
C++ Code:
#include <iostream> using namespace std; int main(int argc, char const *argv[]) { int size, r, c; //Taking size as input from user cout << "Size : "; cin >> size; //Taking middle of the pattern in negative int mid = -size / 2 + 1; //Outer Loop for (r = 1; r <= size; r++) { //Inner loop for (c = 1; c <= size; c++) { if (c == size / 2 + 1 || c == mid || c == size - mid + 1) cout << "*"; else cout << " "; } //Prints a newline cout << endl; //Incrementing the mid value mid++; } return 0; }
Output: Size : 5 * * * * * *** *
Related Java Star Pattern Programs: