In this post, we will learn to create two Right angle triangle sing control statements using For Loop and While Loop. The program used two loop which will help to display triangle.
To understand these programs you should have knowledge of the following concepts of Core Java Programming.
- For Loop
- While and do-while Loop
- Java if..else Statement
How to Display Right angle triangle using number?
- Define the value of rows.
- Now run the first loop till value.
- And the second loop using steps.
- Print the numbers.
Example:
- Input:
- Input number of rows : 5
- Output:
- 1
- 12
- 123
- 1234
- 12345
- Input:
- Input number of rows : 7
- Output:
- 1
- 12
- 123
- 1234
- 12345
- 123456
- 1234567
Example 1: Program to Display the Right Angle Triangle using for loop
This program allows the user to enter the number of rows. Next, this java program using for loop to print Right Angle Triangle for that we are going to use two java for a loop. First loop will work till entering row value and the second loop till the first loop pass the number as shown.
import java.util.Scanner; public class demo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the valu of rows: "); int num = sc.nextInt(); for (int i=1;i<=num;i++){ for (int j=1;j<=i;j++){ System.out.print(j); } System.out.println(); } } }
Output:
Input number of rows : 10 1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678910
Example 2: Program to Display the Right Angle Triangle using while loop
This Java Program to display the right angle triangle same as the above program take input from the user.
import java.util.Scanner; public class right_angle_triangle_using_number { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Input number of rows : "); int num = sc.nextInt(); int i=1; while (i<=num){ int j=1; while (j<=i){ System.out.print(j); j++; } System.out.println(); i++; } } }
Output:
Input number of rows : 7 1 12 123 1234 12345 123456 1234567
- Java Program to Display the weekday between 1 and 7
- Java Program to Find the number of days in a Months.
- Java Program to Print character is vowel or Consonant
- Java Program to Check a year is a leap year or not
- Java Program to display the sum of first N natural numbers
- Java Program to find sum and average of N numbers
- Java Program to find a cube of an N given numbers.
- Java Program to Print sum of first N odd natural numbers
- Java Program to Check if Number is Positive or Negative