Hollow Inverted Triangle| Write a program in java to print a hollow inverted triangle. | ISC Computer Science | ICSE Computer Application

Image is Unavailable

Algorithm

  1.   Start
  2. n:=0
  3. Print "Enter a number"
  4. Read n
  5. i=0
  6. If i<=n, go to step 7 or else go to step 21
  7. j=0
  8. If j<i, go to step 9 or else go to step 11
  9. Print " "
  10. j++. Go to step 8
  11. k=0
  12. If k<(n-i)*2, go to step 13 or else go to step 17
  13. If (i==0||k==0||k==((n-i)*2)-1), go to step 14 or else go to step 15.
  14. Print "*". Go to step 16
  15. Print " "
  16. k++. Go to step 12
  17. If i==n, go to step 18 or else go to step 19
  18. Print "*"
  19. Go to next line.
  20. i++. Go to step 6
  21. Stop

Program

Buffered Reader

import java.io.*;
class HollowInvertedTriangle
{
    int n; 
    void accept() throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter a number");
        n=Integer.parseInt(br.readLine());
    }
    
    void display()
    {
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<i;j++)
            {
                System.out.print(" ");
            }
            for(int k=0;k<(n-i)*2;k++)
            {
                if(i==0||k==0||k==((n-i)*2)-1)
                {
                    System.out.print("*");
                }
                else
                {
                    System.out.print(" ");
                }
            }
            if(i==n)
            {
                System.out.println("*");
            }
            System.out.println();
        }
    }
    
    void main() throws IOException
    {
        accept();
        display();
    }
}

Scanner

import java.util.*;
class HollowInvertedTriangle
{
    int n; 
    void accept() 
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a number");
        n=sc.nextInt();
    }
    
    void display()
    {
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<i;j++)
            {
                System.out.print(" ");
            }
            for(int k=0;k<(n-i)*2;k++)
            {
                if(i==0||k==0||k==((n-i)*2)-1)
                {
                    System.out.print("*");
                }
                else
                {
                    System.out.print(" ");
                }
            }
            if(i==n)
            {
                System.out.println("*");
            }
            System.out.println();
        }
    }
    
    void main() 
    {
        accept();
        display();
    }
}

Documentation

Variable Description

 Data TypeVariable NameDescription
 intTo store the height of the triangle 
 intTo act as a counter variable 
 intTo act as a counter variable 

Method Description

 Return TypeMethod NameDescription
 voidaccept() To accept data from the user  
 voiddisplay() To display a hollow inverted triangle 
 voidmain() To act as an entry gateway for the compiler 

Output



Image is unavailable


Share with all ISC Computer Science Students you know.

Comment below with all your program requests and blog suggestions.

Follow us on social media by clicking on these links. 
Instagram : @ISCJavaCode
Twitter: @ISCJavaCode
Reddit: @ISCJavaCode
Pinterest: @ISCJavaCode
Facebook: @ISCJavaCode


Stay Healthy
Stay Happy
Stay Helpful



Comments

You might also like:

Conversion of Complex Sentences to Simple Sentences

Conversion of Compound Sentences to Complex Sentences.

Conversion of Complex Sentences to Compound Sentences