Registered User
Registered User

Reputation: 474

Print a triangular pattern of asterisks

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

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

n = 0

print ("Pattern A")
for x in range (0,11):
    n = n + 1
    for a in range (0, n-1):
        print ('*', end = '')
    print()
print ('')
print ("Pattern B")
for b in range (0,11):
    n = n - 1
    for d in range (0, n+1):
        print ('*', end = '')
    print()
print ('')

When i start pattern C and D, i try the following:

print ("Pattern C")
for e in range (11,0,-1):
    n = n + 1
    for f in range (0, n+1):
        print ('*', end = '')
    print()
print ('')
print ("Pattern D")
for g in range (11,0,-1):
    n = n - 1
    for h in range (0, n-1):
        print ('*', end = '')
    print()

But the result is the same as A and B. Help is appreciated!

Upvotes: 3

Views: 63837

Answers (15)

Kumaravel K
Kumaravel K

Reputation: 1

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

Output :

  *
 **
***



Upvotes: 0

kuldeep chopra
kuldeep chopra

Reputation: 782

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)

Upvotes: 1

Prajakta Kale
Prajakta Kale

Reputation: 391

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();
                }
            }
        }

Upvotes: 1

Kushini Hathurusinghe
Kushini Hathurusinghe

Reputation: 11

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

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

Upvotes: 1

Alon Eilat
Alon Eilat

Reputation: 53

The function:

def arrow(my_char, max_length):
    for i in range(max_length*2):
        if i <= max_length:
            print(my_char*i)
        if i > max_length:
            print(my_char*(max_length*2-i))

The main():

def main():
    print(arrow('*', 8))

if __name__ == "__main__":
    main()

Output:

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

Upvotes: 1

himanshu
himanshu

Reputation: 41

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()  

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

Upvotes: 1

Damini Amin
Damini Amin

Reputation: 11

using spaces you can create different- different patterns: 1.

def pattern(limit):

    for i in range(limit+1):
        print((limit-i)*"  "+ ' #'*i)    

pattern(4)

Output: 
       #
     # #
   # # #
 # # # #

2.remove one space from above code and pattern will be changed in a pyramid

def pattern(limit):
    for i in range(limit+1):
        print((limit-i)*" "+ ' #'*i)       
pattern(4)

output: #
       # #
      # # #
     # # # # 

3: no space print((limit-i)*""+ ' #'*i) will create

 #
 # #
 # # #
 # # # #

Upvotes: 1

Greendaddy
Greendaddy

Reputation: 61

print("Pattern C")
n=int(input("Enter number of rows: ")) 
for i in range (n,0,-1):
    print((n-i) * ' ' + i * '*')

Upvotes: 2

Mohan
Mohan

Reputation: 4829

Pythonic way of doing this. Just 1 line of code (using list comprehension) for each pattern.

n = 10

# for A
print('\n'.join(['*'*i for i in range(1,n+1)]))

#output
*
**
***
****
*****
******
*******
********
*********
**********


# for B
print('\n'.join(['*'*i for i in range(n+1,0,-1)]))

#output
***********
**********
*********
********
*******
******
*****
****
***
**
*

# for C
print('\n'.join([' '*(n-i) + '*'*i for i in range(n,0,-1)]))

#output
**********
 *********
  ********
   *******
    ******
     *****
      ****
       ***
        **
         *


# for D
print('\n'.join([' '*(n-i) + '*'*i for i in range(1,n+1)]))

#output
         *
        **
       ***
      ****
     *****
    ******
   *******
  ********
 *********
**********

Upvotes: 2

omkar
omkar

Reputation: 11

def fun (n):
    return n
n=int(raw_input("Enter the number"))
print "The given number is",n
for i in range (n,0,-1):
    print "*"*i

output:

The given number is 3
***
**
*

Upvotes: 1

Rajesh Mishra
Rajesh Mishra

Reputation: 21

for i in range (1,n):    # loop will execute from 1 to 4(n-1)
    for j in range(1,i+1):
        print("*",end="")
    print()
for i in range (n+1,1,-1):
    for j in range(i,1,-1):
        print("*",end="")
    print()

using for loop

Upvotes: 1

Rajesh Mishra
Rajesh Mishra

Reputation: 21

i=0
while(i<5):
    j=0
    while(j<=i):
        print("*",end="") #end="" is given to stay on same line
        j=j+1
    print("")  #it will take u to new line
    i=i+1
    j=0
i=i-2
j=i
while(i>=0):
    while(j>=0):
        print("*",end="")
        j=j-1
    print() #will also work
    i=i-1
    j=i

this will also work

Upvotes: 1

Ujjawal Khare
Ujjawal Khare

Reputation: 796

learn an easy way:
code1:

for n in range(0,5):
    n +=1
    print ("*" *(0+n))

what it does:

  1. for n in range(0,5): --> call a range of no of times you want loop to execute
  2. increment value of int n by one, so starting from range 0 to 5 every time for loop runs it increments int n by 1
  3. now print string "" and multiply it by value of int n+0, So when int n=0 as per logic int n increments by 1 and print ("" (0+n)) = print ("" *(0+1)) = *

output:
*
**
***
****
*****

code2:

for n in range(-5,0):
    n +=1
    print ("*" *(0-n+1))

output:
****
***
**
*

Upvotes: 1

khushbu
khushbu

Reputation: 164

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

Upvotes: 3

John1024
John1024

Reputation: 113844

Both patterns C and D require leading spaces and you are not printing any spaces, just stars.

Here is alternative code that prints the required leading spaces:

print ("Pattern C")
for e in range (11,0,-1):
    print((11-e) * ' ' + e * '*')

print ('')
print ("Pattern D")
for g in range (11,0,-1):
    print(g * ' ' + (11-g) * '*')

Here is the output:

Pattern C
***********
 **********
  *********
   ********
    *******
     ******
      *****
       ****
        ***
         **
          *

Pattern D

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

In more detail, consider this line:

print((11-e) * ' ' + e * '*')

It prints (11-e) spaces followed by e stars. This provides the leading spaces needed to make the patterns.

If your teacher wants nested loops, then you may need to convert print((11-e) * ' ' + e * '*') into loops printing each space, one at a time, followed by each star, one at a time.

Pattern C via nested loops

If you followed the hints I gave about nested loops, you would have arrived at a solution for Pattern C like the following:

print ("Pattern C")
for e in range (11,0,-1):
    #print((11-e) * ' ' + e * '*')
    for d in range (11-e):
        print (' ', end = '')
    for d in range (e):
        print ('*', end = '')
    print()

Upvotes: 4

Related Questions