Java code to print Asterisks (*) based on Input Provides
e.g: n = 5
then o/p will be as -
CODE:
public class AstrikPrint
{
public static void main(String[] args)
{
int n = 5;
int count = 1;
for (int i = 0; i<n/2; i++ )
{
for (int j = 0; j < count; j++)
{
System.out.print("*");
}
count+=2;
System.out.print("\n");
}
for (int j = 0; j < count; j++)
{
System.out.print("*");
}
count-=2;
System.out.print("\n");
for (int i = 0; i<n/2; i++ )
{
for (int j = count; j >0; j--)
{
System.out.print("*");
}
count-=2;
System.out.print("\n");
}
}
}
e.g: n = 5
then o/p will be as -
CODE:
public class AstrikPrint
{
public static void main(String[] args)
{
int n = 5;
int count = 1;
for (int i = 0; i<n/2; i++ )
{
for (int j = 0; j < count; j++)
{
System.out.print("*");
}
count+=2;
System.out.print("\n");
}
for (int j = 0; j < count; j++)
{
System.out.print("*");
}
count-=2;
System.out.print("\n");
for (int i = 0; i<n/2; i++ )
{
for (int j = count; j >0; j--)
{
System.out.print("*");
}
count-=2;
System.out.print("\n");
}
}
}
No comments:
Post a Comment