Program to Print Alphabet F Star Pattern
In this article we will print alphabet F star pattern.
Example-1 When row value=5 ****** * **** * *
Example-2 When row value=6 ******* * * ***** * *
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 total row and store it in an integer variable
row
. - Take first for loop to print the row value and a star for each row.
- Take first inner for loop to print column value i.e., star according to condition
if ((r == 0) || (r == row / 2 && c <= row / 2))
- Then go on printing the star symbol according to loop.
JAVA Code:
Method-1 : Static Star Character
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(); //outer for loop for (r = 0; r < row; r++) { System.out.print("*"); // inner for loop for (c = 0; c < row; c++) { // printing star if ((r == 0) || (r == row / 2 && c <= row / 2)) System.out.print("*"); // printing space else continue; } System.out.print("\n"); } } }
Output : Enter row : 5 ****** * **** * *
Method-2 : User Input Character
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 f; //creating object Scanner s = new Scanner(System.in); // entering the number of row System.out.print("Enter rows : "); row = s.nextInt(); // entering any random character System.out.print("Enter character : "); f = s.next().charAt(0); //outer for loop for (r = 0; r < row; r++) { System.out.print(f); // inner for loop for (c = 0; c < row; c++) { // printing star if ((r == 0) || (r == row / 2 && c <= row / 2)) System.out.print(f); // printing space else continue; } System.out.print("\n"); } } }
Output : Enter row : 5 Enter character : # ###### # #### # #
C Code:
#include <stdio.h> int main() { int r, row, c ,d; printf("Enter rows: "); scanf("%d", &row); for (r = 0; r < row; r++) { printf("*"); for (c = 0; c < row; c++) { if ((r == 0) || (r == row / 2 && c <= row / 2)) printf("*"); else continue; } printf("\n"); } return 0; }
Output : Enter row : 5 ****** * **** * *
C++ Code:
#include <iostream> using namespace std; int main() { int row, r , c ,d ; cout << "Enter rows: "; cin >> row; for (r = 0; r < row; r++) { cout << "*"; for (c = 0; c < row; c++) { if ((r == 0) || (r == row / 2 && c <= row / 2)) cout << "*"; else continue; } cout << "\n"; } return 0; }
Output : Enter row : 5 ****** * **** * *
Related Java Star Pattern Programs: