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

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

Source Code:

/* Program to print Floyd Triangle*/
import java.util.*;
class FloydTriangle
{
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,x=1;
System.out.println("Floyd Triangle: \n");

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

}
}

Output:

Enter Any Character: ?

 Enter Size Limit: 5
 Floyd Triangle: 

?
??
???
????
?????

 Enter Any Character: $

 Enter Size Limit: 7
 Floyd Triangle: 

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

[yuzo_views]



CopyRight © 2019

CopyRight © 2019