Java program to print the character or a letter x using star

Java program to print the character or a letter x using a star. Using this program in java we can print the letter or string x in the output as a pattern. 

A pattern is someone or something that is used as a template to create a copy, a design, or an expected activity. A pattern is the paper parts that a seamstress uses to build a dress; a dress pattern is an example of a pattern.

import java.util.*;
class Main
{
public static void main(String args[])
{
int i,j,n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number to print the character");
n = sc.nextInt();
for(i=n;i>=1;i--)
{
for(j=i;j<n;j++)
{
System.out.print(" ");
}
for(j=1;j<=(2*i-1);j++)
{
if(j==1 || j==(2*i-1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println("");
}
for(i=2;i<=n;i++)
{
for(j=i;j<n;j++)
{
System.out.print(" ");
}
for(j=1;j<=(2*i-1);j++)
{
if(j==1 || j==(2*i-1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println("");
}
}
}

Output:

Enter the number to print the character
5

 

*                     *
*                *
*           *
*     *
*
*     *
*          *
*               *
*                     *

Looping

A loop is used to repeatedly execute a set of statements until a certain condition is met. When showing numbers from 1 to 100, for example, you might wish to set the value of a variable to 1 and display it 100 times, raising its value by 1 with each loop iteration.

Iterations

An Iterator is a collection looping object that can be used with ArrayList and HashSet. It’s termed an “iterator” since looping is referred to as “iterating.” You must import an Iterator from java.util package in order to use it.

What is for loop and if else in python program

The startup of the loop, when we set our counter to a beginning value. Before the loop starts, the initialization statement is executed. The test statement determines whether or not a particular condition is true.

For Loop 

A for-loop is a control flow statement in computer science that specifies iteration and allows code to be executed repeatedly. When the number of iterations is known before entering the loop, for-loops are commonly utilized.

If-Else

If statements in Python handle conditional statements, and we’ve already seen how to use conditional statements in other ways, such as with Python if-else. “if condition” – This is used when one of the conditions is true or false and you need to print the result.