In the following example, we’ll create a program that prints Inverted Pascal Triangle using any character that is given as input from the user. The Program thus prints an Inverted Pascal Triangle out of that character.

The basic idea is to first implement the logic of Inverted Pascal Triangle as usual and replace the Loop Control Variable that originally prints, with the input Character.

Source Code:

/* Program to print Inverted Pascal Triangle*/
import java.util.*;
class InvPascalTriangle
{
public static void main()
{
Scanner inp=new Scanner(System.in);
System.out.print("\n Enter Any Character: ");
char c=((inp.nextLine()).charAt(0));
System.out.print("\n Enter Size Limit: ");
int n=inp.nextInt();
int i,j,k;
System.out.println("Inverted Pascal Triangle: \n");

for(i=n;i>=1;i--)
{
for(k=0;k<=(n-i);k++)
  System.out.print(" ");

for(j=i;j<=(2*i)-1;j++)
  System.out.print(c);

for(j=1;j<=(i-1);j++)
  System.out.print(c);
  
System.out.println();
}

}
}

Output:

Enter Any Character: $

Enter Size Limit: 6
Inverted Pascal Triangle: 

 $$$$$$$$$$$
  $$$$$$$$$
   $$$$$$$
    $$$$$
     $$$
      $

Enter Any Character: @

Enter Size Limit: 4
Inverted Pascal Triangle: 

 @@@@@@@
  @@@@@
   @@@
    @

[yuzo_views]



CopyRight © 2019

CopyRight © 2019