In this article we are going to see how to print the upward 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.
Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.
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 newline.
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 row of the pattern 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 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 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: