Program to Print Greater Than Symbol Star Pattern
In this article we are going to see how to print the greater than 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.
Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.
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:
Method-1: Static Star Character
import java.util.Scanner; class Main { 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(); int d = 1; //Outer Loop for (r = 1; r <= size; r++) { //Inner loop for (c = 1; c <= size; c++) { if (c == d) System.out.print("*"); else System.out.print(" "); } //Prints a newline System.out.println(); //Adjusting the d value if (r <= size / 2) d++; else d--; } } }
Output: Size : 5 * * * * *
Method-2: User Input Character
import java.util.Scanner; class Main { public static void main(String[] args) { int size, r, c; Scanner scan = new Scanner(System.in); //Taking size as input from user System.out.print("Size : "); size = scan.nextInt(); //Taking any random character as input from user System.out.print("Size : "); char greater = scan.next().charAt(0); int d = 1; //Outer Loop for (r = 1; r <= size; r++) { //Inner loop for (c = 1; c <= size; c++) { if (c == d) System.out.print(greater); else System.out.print(" "); } //Prints a newline System.out.println(); //Adjusting the d value if (r <= size / 2) d++; else d--; } } }
Output Size : 5 Character : > > > > > >
C Code:
#include <stdio.h> int main() { int size, r, c; //Taking size as input from user printf("Size : "); scanf("%d", &size); int d = 1; //Outer Loop for (r = 1; r <= size; r++) { //Inner loop for (c = 1; c <= size; c++) { if (c == d) printf("*"); else printf(" "); } //Prints a newline printf("\n"); //Adjusting the d value if (r <= size / 2) d++; else d--; } 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; int d = 1; //Outer Loop for (r = 1; r <= size; r++) { //Inner loop for (c = 1; c <= d; c++) { if (c == d) cout << "*"; else cout << " "; } //Prints a newline cout << endl; //Adjusting the d value if (r <= size / 2) d++; else d--; } return 0; }
Output: Size : 5 * * * * *
Related Java Star Pattern Programs: