Program to Print Less Than Symbol Star Pattern
In this article we are going to see how to print the less 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.
Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output
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(); //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 == mid) System.out.print("*"); else System.out.print(" "); } //Prints a newline System.out.println(); //Adjusting the mid value if (r <= size / 2) mid--; else mid++; } } }
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("Character : "); char less = scan.next().charAt(0); //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 == mid) System.out.print (less); else System.out.print (" "); } //Prints a newline System.out.println (); //Adjusting the mid value if (r <= size / 2) mid--; else mid++; } } }
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); //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 == mid) printf("*"); else printf(" "); } //Prints a newline printf("\n"); //Adjusting the mid value if (r <= size / 2) mid--; else 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 <= mid; c++) { if (c == mid) cout << "*"; else cout << " "; } //Prints a newline cout << endl; //Adjusting the mid value5 if (r <= size / 2) mid--; else mid++; } return 0; }
Output: Size : 5 * * * * *
Related Java Star Pattern Programs: