Java program to print a right-angled triangle:
This post will show you how to print a right-angled triangle in Java. We will use any character or number to print a right angled triangle. Before we move to the program, let me show you how the algorithm works.
Algorithm to print a right-angled triangle:
A right-angled triangle of height 5 looks as like below if we use * to print it:
*
* *
* * *
* * * *
* * * * *
Here, each nth row has n number of star i.e. for the first row, it has one star, for the second row it has two star etc. We have to use two loops to print the triangle. The outer loop will point to each row of the triangle and the inner loop will print the body of the triangle.
We will use the below algorithm:
- Take the height of the triangle as input from the user.
- Run one loop for height number of times. It will run from 1 to height.
- Run one inner loop. It will run for the number of time the outer loop is on. For example, for the first iteration of the outer loop, the inner loop will run only one time. For the second iteration of the outer loop, the inner loop will run for two times etc.
- Print the character in the inner loop that is used to print the triangle.
Let’s write down the program:
Example 1: Java program to print a right-angled triangle by using for loops:
Let’s use for loops to print the right-angled triangle by using for loops in Java:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
int height;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the height of the triangle: ");
height = sc.nextInt();
System.out.println();
for (int i = 1; i <= height; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*" + " ");
}
System.out.println();
}
}
}
Here,
- The height of the triangle is taken as input from the user. It is stored in the variable height.
- Two for loops are used to print the triangle.
- The outer loop runs from i = 1 to i = height. The inner loop runs from j = 1 to j = i. Inside the inner loop, it prints the star with a blank space to its right.
- At the end of each iteration of the outer loop, it prints a new line.
If you run the above program, it will print output as like below:
Enter the height of the triangle:
5
*
* *
* * *
* * * *
* * * * *
Enter the height of the triangle:
10
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
Example 2: Java program to print a right-angled triangle by using for loops with any character:
We can also take the character as input from the user and use that character to print the triangle. Let’s modify the above program to read the character as input from the user:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
int height;
String c;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the height of the triangle: ");
height = sc.nextInt();
System.out.println("Enter the character to print the triangle: ");
c = sc.next();
System.out.println();
for (int i = 1; i <= height; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(c + " ");
}
System.out.println();
}
}
}
Here, one new string variable c is added. This variable is used to print the triangle. It will give output as like below:
Enter the height of the triangle:
5
Enter the character to print the triangle:
$
$
$ $
$ $ $
$ $ $ $
$ $ $ $ $
Enter the height of the triangle:
10
Enter the character to print the triangle:
^
^
^ ^
^ ^ ^
^ ^ ^ ^
^ ^ ^ ^ ^
^ ^ ^ ^ ^ ^
^ ^ ^ ^ ^ ^ ^
^ ^ ^ ^ ^ ^ ^ ^
^ ^ ^ ^ ^ ^ ^ ^ ^
^ ^ ^ ^ ^ ^ ^ ^ ^ ^
Enter the height of the triangle:
6
Enter the character to print the triangle:
.
.
. .
. . .
. . . .
. . . . .
. . . . . .
Example 3: Java program to print a right-angled triangle by using while loops:
We can also replace the for loops with while loops. Let me rewrite the above program with while loop:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
int height, i = 1, j;
String c;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the height of the triangle: ");
height = sc.nextInt();
System.out.println("Enter the character to print the triangle: ");
c = sc.next();
System.out.println();
while (i <= height) {
j = 1;
while (j <= i) {
System.out.print(c + " ");
j++;
}
System.out.println();
i++;
}
}
}
This program uses two while loops to print the triangle instead of for loops. The only difference is how the loop works. We are initializing the values of i and j before the loops starts and at the end of the loop, the values are incremented by 1.
If you run this app, it will print similar output.
Enter the height of the triangle:
4
Enter the character to print the triangle:
@
@
@ @
@ @ @
@ @ @ @
Example 4: By using a separate method:
We can use a separate method to print the right-angled triangle. This method will take the height and the character as parameters and it will print the triangle in the method.
import java.util.Scanner;
class Main {
private static void printTriangle(int height, String c) {
int i = 1, j;
while (i <= height) {
j = 1;
while (j <= i) {
System.out.print(c + " ");
j++;
}
System.out.println();
i++;
}
}
public static void main(String[] args) {
int height;
String c;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the height of the triangle: ");
height = sc.nextInt();
System.out.println("Enter the character to print the triangle: ");
c = sc.next();
System.out.println();
printTriangle(height, c);
}
}
- printTriangle is the method to print the triangle. It takes the height and the character as its parameters.
- We are using while loops to print the triangle.
It will give similar result.
You might also like:
- Java program to check if a year is a leap year or not
- 4 different Java program to find a random value in an array
- Java program to find the first repeating character in a string
- Java program to check if two strings are an anagram or not
- Java program to print the harmonic series
- Java program to convert a Hexadecimal value to Decimal
- 3 ways in Java to print a Fibonacci triangle