Monday 24 August 2020

V

 

#Program to Print alphabet 'V' in Star Pattern

Expected Output:

*       *
 *     * 
  *   *  
   * *   
    *    


Code in Java:
public class V 
{
	public static void main(String[] args) 
	{
    	//px and py used to increment and decrement rows and columns
		int n=5, px=1, py=n*2-1;
		for(int i=1; i<=n; i++)//Outer loop for number of rows
		{
			for(int j=1; j<n*2; j++)//inner loop for number of columns
			{
			//if condition to print the V pattern Stars
			if(j==px||j==py) 
			{
				System.out.print("*");//printing stars
			}
			else
			{
				System.out.print(" ");//printing white spaces
			}
			}
		px++;
		py--;
		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