Print a triangular pattern of asterisks

后端 未结 15 963
慢半拍i
慢半拍i 2020-11-30 11:01

I am required to use nested for loops and print(\'*\', end=\' \') to create the pattern shown here:

And here is my code. I have figured out the first t

相关标签:
15条回答
  • 2020-11-30 11:26

    Pattern 1

    for x in range(1,11):
        print("*"*x)
    

    Output

    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********
    

    Pattern 2

    for x in range(10,0,-1):
        print("*"*x)
    

    Output

    **********
    *********
    ********
    *******
    ******
    *****
    ****
    ***
    **
    *
    

    Pattern 3

     i=0
        for x in range(10,0,-1):
                print(" "*i,end="")
    
    
           print("*"*x)
            i+=1
    

    Output

    **********
     *********
      ********
       *******
        ******
         *****
          ****
           ***
            **
             *
    

    Pattern 4

    i=10
    for x in range(1,11):
            print(" "*i,end="")
            print("*"*x)
            i-=1
    

    Output

              *
             **
            ***
           ****
          *****
         ******
        *******
       ********
      *********
     **********
    
    0 讨论(0)
  • 2020-11-30 11:27

    for i in range(1,7): print(" "(7-i)+""*(i))

    Output :

      *
     **
    ***
    



    0 讨论(0)
  • 2020-11-30 11:28

    Try this one!

      a)
                *                                                                                                                                                    
                **                                                                                                                                                   
                ***                                                                                                                                                  
                ****                                                                                                                                                 
                *****  
    
                public class Main { 
                    public static void main(String[] args) {
                        int n=5;
                        for(int i=1;i<=n;i++)
                        {
                            for(int j=1;j<=i;j++)
                            {
                               System.out.print("*"); 
                            }
                            System.out.println();
                        }
                    }
                 }
    
                b)
                *****                                                                                                                                                
                 ****                                                                                                                                                
                  ***                                                                                                                                                
                   **                                                                                                                                                
                    *
    
    
                public class Main {
                public static void main(String[] args) {
                    int n=5;
                    for(int i=1;i<=n;i++)
                    {
                            for(int j=1;j<=(i-1);j++)
                            {
                               System.out.print(" "); 
                            }
                            for(int j=i;j<=n;j++)
                            {
                                System.out.print("*");
                            }
                            System.out.println();
                    }
                }
             }
    
            c)
                *                                                                                                                                                
               **                                                                                                                                                
              ***                                                                                                                                                
             ****                                                                                                                                                
            *****
    
            public class Main
            {
                public static void main(String[] args)
                {       int n=5;
                        for(int i=n;i>0;i--)        
                        {
                           for(int j=1;j<=(i-1);j++)
                           {
                              System.out.print(" "); 
                           }
                           for(int j=i;j<=n;j++)
                           {
                              System.out.print("*");
                           }
                           System.out.println();
                    }
                }
             }
    
            d)
            *****                                                                                                                                                
            ****                                                                                                                                                 
            ***                                                                                                                                                  
            **                                                                                                                                                   
            * 
    
    
            public class Main
            {
                public static void main(String[] args) {
                    int n=5;
                    for(int i=n;i>0;i--)
                    {
                        for(int j=1;j<=i;j++)
                        {
                            System.out.print("*");
                        }
                        System.out.println();
                    }
                }
            }
    
    0 讨论(0)
  • 2020-11-30 11:28
    var n = 5;
    
        for(int row = 0 ; row < n; row++)
        {
            for(int col = 1; col <= n; col++)
            {
                if(col < n - row)
                {
                    Console.Write(" ");
                }
                else
                {
                    Console.Write("*");
                }
    
            }
            Console.WriteLine();
    
        }
    

    (D)

    0 讨论(0)
  • 2020-11-30 11:29
    for i in range(0,5):
    for j in range(0,i+1):
        print("*",end="")
    print()
    for k in range(5,0,-1):
    for l in range(k-1,0,-1):
        print("*",end="")
    print()  
    

    *
    **
    ***
    ****
    *****
    ****
    ***
    **
    *

    0 讨论(0)
  • 2020-11-30 11:31
    for i in range(1,n+1): print '{:>{}}'.format('#'*i, n)
    this is For pattern D
    
    
    for i in range(1,n-1): print '{:>{}}'.format('#'*i, n)
    this is For pattern c
    
    0 讨论(0)
提交回复
热议问题