Monday 24 August 2020

X

 

#Program to Print alphabet 'X' in Star Pattern

Expected Output:

*        *
  *    * 
     *  
  *    * 
*        *


Code in Java:
public class X 
{
	public static void main(String[] args) 
	{
		for(int i=1; i<=5; i++)//Outer loop for number of rows
		{
			for(int j=1; j<=5;j++)//inner loop for number of columns
			{
			//if condition to print the X pattern Stars
			if(i==j||i==1&&j==5||i==2&&j==4||i==4&&j==2||i==5&&j==1)
			{
				System.out.print("*");//printing stars
			}
			else
			{
				System.out.print(" ");//printing white spaces
			}
			}
		System.out.println();
		}
		System.out.println();//printing newline after each row
	}
}      
      

No comments:

Post a Comment

If you have any doubts, please let me know

Pattern 31 & 32