Program to Print Alphabet I Star Pattern
In this article we are going to see how to print I star program.
Example-1 When row value=6 ****** * * * * ******
Example-2: When row 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 total row and store it in an integer variable
row. - Take first for loop to print the row value .
- Take first inner for loop to print column value i.e., star according to condition
if (r == 0 || r == row - 1) and if (c == row / 2)else loop will print space . - 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=0;
//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++)
{
for (c = 0; c < row; c++)
{
if (r == 0 || r == row - 1)
System.out.print ("*");
else if (c == row / 2)
System.out.print ("*");
else
System.out.print (" ");
}
System.out.print ("\n");
}
}
}
Output: Enter rows : 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=0;
char i;
//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 : ");
i = s.next().charAt(0);
//outer for loop
for (r = 0; r < row; r++)
{
for (c = 0; c < row; c++)
{
if (r == 0 || r == row - 1)
System.out.print (i);
else if (c == row / 2)
System.out.print (i);
else
System.out.print (" ");
}
System.out.print ("\n");
}
}
}
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 = 0; r < row; r++)
{
for (c = 0; c < row; c++)
{
if (r == 0 || r == row - 1)
printf("*");
else if (c == row / 2)
printf("*");
else
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 = 0; r < row; r++)
{
for (c = 0; c < row; c++)
{
if (r == 0 || r == row - 1)
cout << "*";
else if (c == row / 2)
cout << "*";
else
cout << " ";
}
cout << "\n";
}
return 0;
}
Output: Enter rows : 5 ***** * * * *****
Related Java Star Pattern Programs: