Program to Print Digit 8 Star Pattern
In this article we are going to see how to print the number ‘8’ star pattern.
Example-1 When size value=5 *** * * * * * * *** * * * * * * ***
Example-2: When size value=8 ****** * * * * * * * * * * * * ****** * * * * * * * * * * * * ******
Now, let’s see the actual program to print it.
Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.
Approach:
- Enter the size and store it in an integer variable size.
- Take first for loop to print all rows.
- Take second/inner for loop to print column values.
- Then go on printing the star symbols according to the iteration and if else condition.
JAVA Code:
Method-1 : Static Star Character
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Size : ");
//Taking size as input from user
int r, c, size=scan.nextInt();
int k = size*2-1;
for(r = 1; r<=k; r++)
{
//Outer loop
if(r==1||r==size||r==k)
for(c = 1;c<=size;c++)
{
//Inner Loop 1
if(c==1||c==size)
System.out.print(" ");
else
System.out.print("*");
}
else
for(c = 1;c<=size;c++)
{
//Inner loop 2
if(c==1||c==size)
System.out.print("*");
else
System.out.print(" ");
}
//Prints a new line
System.out.println();
}
}
}
Output: Size : 5 *** * * * * * * *** * * * * * * ***
Method-2 : User Input Character
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
int r, c, size;
char symbol;
Scanner scan = new Scanner(System.in);
System.out.print("Size : ");
//Taking size as input from user
size=scan.nextInt();
System.out.print("Character : ");
//Taking size as input from user
symbol=scan.next().charAt(0);
int k = size*2-1;
for(r = 1; r<=k; r++)
{//Outer loop
if(r==1||r==size||r==k)
for(c = 1;c<=size;c++)
{//Inner Loop 1
if(c==1||c==size)
System.out.print(" ");
else
System.out.print(symbol);
}
else
for(c = 1;c<=size;c++)
{//Inner loop 2
if(c==1||c==size)
System.out.print(symbol);
else
System.out.print(" ");
}
//Prints a new line
System.out.println();
}
}
}
Output: Size : 5 Character : 8 888 8 8 8 8 8 8 888 8 8 8 8 8 8 888
C Code:
#include <stdio.h>
int main(int argc, char const *argv[])
{
printf("Size : ");
//Taking size as input from user
int size, r, c;
scanf("%d", &size);
int k = size * 2 - 1;
for (r = 1; r <= k; r++)
{ //Outer loop
if (r == 1 || r == size || r == k)
for (c = 1; c <= size; c++)
{ //Inner Loop 1
if (c == 1 || c == size)
printf(" ");
else
printf("*");
}
else
for (c = 1; c <= size; c++)
{ //Inner loop 2
if (c == 1 || c == size)
printf("*");
else
printf(" ");
}
//Prints a new line
printf("\n");
}
return 0;
}
Output: Size : 5 *** * * * * * * *** * * * * * * ***
C++ Code:
#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
cout << "Size : ";
//Taking size as input from user
int size, r, c;
cin >> size;
int k = size * 2 - 1;
for (r = 1; r <= k; r++)
{ //Outer loop
if (r == 1 || r == size || r == k)
//condition for the horizontal lines in 8
for (c = 1; c <= size; c++)
{ //Inner Loop 1
if (c == 1 || c == size)
cout << " ";
else
cout << "*";
}
else
for (c = 1; c <= size; c++)
{ //Inner loop 2
if (c == 1 || c == size)
cout << "*";
else
cout << " ";
}
//Prints a new line
cout << endl;
}
return 0;
}
Output: Size : 5 *** * * * * * * *** * * * * * * ***
Related Java Star Pattern Programs: