Print Triangle using Recursion in Java

Problem:

Write a method that prints triangle using Recursion in Java.

Output:

Not applicable.

Solution:

import java.util.Scanner;
public class Stars 
{
    
    public static void main(String args[])
    {
      Scanner scan=new Scanner(System.in);
      System.out.println("enter the height of the triangle: ");
      int height=scan.nextInt();
      printTriangle(height);
    }
    
    public static String printTriangle (int count) {
    if( count <= 0 ) 
      return"";

        String p = printTriangle(count - 1);
        p = p + "*";
        System.out.print(p);
        System.out.print("\n");

    return p;
     }

  }


1 comment :

  1. laser liposuction Korea's #1 Liposculpture Clinic. Lydian plastic surgery is the home of VIP patients. Celebrities, Influencers and Diplomats all know and trust Doctor An and Lydian plastic surgery clinic to provide detailed results.

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact