How can i print a triangle with “*”s using for loop in java?

前端 未结 9 956
情话喂你
情话喂你 2021-01-29 10:35

I want to draw a triangle with stars like below using for loop, but i really don\'t have any idea of how to do this ? Triangle is going to be like this:

*
**
**         


        
9条回答
  •  后悔当初
    2021-01-29 11:28

              import java.util.*;
    
              class StarForloop
    {
              public static void main(String arg[])
        {
              Scanner ob=new Scanner(System.in); //getting input
              System.out.println("Enter any NO");
    
              int count=ob.nextInt(); 
            String ab="*";  // initialize string variable
    
              for(int i=1; i<=count; i++)
            {
    
               ab=ab+"*"; // here you add one another string
                System.out.println(ab);
    
    
            }           
        }
    }
    

提交回复
热议问题