Program to Print Reverse K Shape Star Pattern
In this article we are going to see how to print the Reverse k-shape star program.
Example-1 When row value=4 **** *** ** * ** *** ****
Example-2: When row value=5 ***** **** *** ** * ** *** **** *****
Now, let’s see program to how 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 total row and store it in an integer variable
row
. - Take first outer for loop to print all rows of upper part .
- First inner for loop will print all spaces of the column
- Second inner for loop to print column values i.e., second inner for loop will print all the stars in the column(in decreasing order).
- Take second outer for loop to print all rows.
- First inner for loop will print all spaces of the column.
- Second inner for loop to print column values i.e., second inner for loop will print all the stars in the column(in increasing order).
JAVA Code:
Method-1 :
import java.util.*; public class Main { public static void main(String args[]) { // taking variable for loop iteration and row . int row,r,c,d; //creating object Scanner s = new Scanner(System.in); // entering the number of row System.out.print("Enter rows : "); row = s.nextInt(); //printing row value for (r = 1; r <= row; r++) { for (c = 1; c < r; c++) System.out.print(" "); for (c = r; c <= row; c++) System.out.print("*"); System.out.println(); } for (r = row -1; r >= 1; r--) { for (c = 2; c <=r; c++) System.out.print(" "); for (c = r; c <= row; c++) System.out.print("*"); System.out.println(); } } }
Output: Enter Rows :5 ***** **** *** ** * ** *** **** *****
Method-2 :
import java.util.*; public class Main { public static void main(String args[]) { // taking variable for loop iteration and row . int row,r,c,d; char gh; //creating object Scanner s = new Scanner(System.in); // entering the number of row System.out.print("Enter rows : "); row = s.nextInt(); // entering tany character System.out.print("Enter character : "); gh = s.next().charAt(0); //printing row value for (r = 1; r <= row; r++) { for (c = 1; c < r; c++) System.out.print(" "); for (c = r; c <= row; c++) System.out.print(gh); System.out.println(); } for (r = row -1; r >= 1; r--) { for (c = 2; c <=r; c++) System.out.print(" "); for (c = r; c <= row; c++) System.out.print(gh); System.out.println(); } } }
Output: Enter Rows : 5 Enter Character : * ***** **** *** ** * ** *** **** *****
C Code:
#include <stdio.h> int main() { int r, row, c ,d; printf("Enter rows: "); scanf("%d", &row); for (r = 1; r <= row; r++) { for (c = 1; c < r; c++) printf(" "); for (c = r; c <= row; c++) printf("*"); printf("\n"); } for (r = row -1; r >= 1; r--) { for (c = 2; c <=r; c++) printf(" "); for (c = r; c <= row; c++) printf("*"); printf("\n"); } return 0; }
Output: Enter Rows :5 ***** **** *** ** * ** *** **** *****
C++ Code:
#include <iostream> using namespace std; int main() { int row, r , c ,d ; cout << "Enter rows: "; cin >> row; for (r = 1; r <= row; r++) { for (c = 1; c < r; c++) cout <<" "; for (c = r; c <= row; c++) cout <<"*"; cout <<"\n"; } for (r = row -1; r >= 1; r--) { for (c = 2; c <=r; c++) cout <<" "; for (c = r; c <= row; c++) cout <<"*"; cout << "\n"; } return 0; }
Output: Enter Rows :5 ***** **** *** ** * ** *** **** *****
Related Java Star Pattern Programs: