Java Program to Display Right angle triangle using number

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:

  1. Input:
  2. Input number of rows : 5
  3. Output:
  4. 1
  5. 12
  6. 123
  7. 1234
  8. 12345
  1. Input:
  2. Input number of rows : 7
  3. Output:
  4. 1
  5. 12
  6. 123
  7. 1234
  8. 12345
  9. 123456
  10. 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

  1. Java Program to Display the weekday between 1 and 7
  2. Java Program to Find the number of days in a Months.
  3. Java Program to Print character is vowel or Consonant
  4. Java Program to Check a year is a leap year or not
  5. Java Program to display the sum of first N natural numbers
  6. Java Program to find sum and average of N numbers
  7. Java Program to find a cube of an N given numbers.
  8. Java Program to Print sum of first N odd natural numbers
  9. Java Program to Check if Number is Positive or Negative

Leave a Comment

Your email address will not be published. Required fields are marked *

x