Monday 21 December 2015

python loop exercises

Write a program to print 10 stars
* * * * * * * * * * 

Approach 1
class Stars:
    def printStars(self, n):
        i=0
        while(i < n):
            print("*",end=" ")
            i+=1

if(__name__=="__main__"):
    obj=Stars()
    obj.printStars(10)


Approach 2
class Stars:
    def printStars(self, n):
        for i in range(n):
            print("*", end=" ")

if(__name__=="__main__"):
    obj=Stars()
    obj.printStars(10)

Write a program to print n*n matrix stars
class Stars:
    def printStars(self, n):
        for i in range(n):
            for j in range(n):
                print("*", end=" ")
            print()

if(__name__=="__main__"):
    obj=Stars()
    obj.printStars(10)
Run above program, you will get following output.
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * *


Write code that print following
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8 9

class Stars:
    def printStars(self, n):
        for i in range(n):
            for j in range(i+1):
                print(j, end=" ")
            print()

if(__name__=="__main__"):
    obj=Stars()
    obj.printStars(10)

Write program to print following    
0 1 2 3 4 5 6 7 8 9
  0 1 2 3 4 5 6 7 8
    0 1 2 3 4 5 6 7
      0 1 2 3 4 5 6
        0 1 2 3 4 5
          0 1 2 3 4
            0 1 2 3
              0 1 2
                0 1
                  0

class Stars:
    def printStars(self, n):
        max=n
        for i in range(n):
            for k in range(i+1):
                print("", end=" ")
            for j in range(max):
                print(j, end=" ")
            print()
            max-=1

if(__name__=="__main__"):
    obj=Stars()
    obj.printStars(10)


Write a program that prints following
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0

class Stars:
    def printStars(self, n):
        max=n
        for i in range(n):
            for j in range(max):
                print(j, end=" ")
            print()
            max-=1

if(__name__=="__main__"):
    obj=Stars()
    obj.printStars(10)

Write a program that print following    
1   2   3   4   5   6   7   8   9
2   4   6   8  10  12  14  16  18
3   6   9  12  15  18  21  24  27
4   8  12  16  20  24  28  32  36
5  10  15  20  25  30  35  40  45
6  12  18  24  30  36  42  48  54
7  14  21  28  35  42  49  56  63
8  16  24  32  40  48  56  64  72
9  18  27  36  45  54  63  72  81

class Stars:
    def printData(self, n):
        for i in range(1, n):
            for j in range(1, n):
                print(i*j, end=" ")
            print()

if(__name__=="__main__"):
    obj=Stars()
    obj.printData(10)

Write a program to print following    
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1

class Stars:
    def printData(self, n):
        for i in range(1, n+2):
           for j in range(1, i):
               print(j, end=" ")

           for k in reversed(range(1, i-1)):
               print(k, end=" ")
           print()


if(__name__=="__main__"):
    obj=Stars()
    obj.printData(9)

Write a program to print following    

                  1
                1 2 1
              1 2 3 2 1
            1 2 3 4 3 2 1
          1 2 3 4 5 4 3 2 1
        1 2 3 4 5 6 5 4 3 2 1
      1 2 3 4 5 6 7 6 5 4 3 2 1
    1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
  1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1

class Stars:
    def printData(self, n):
        spaces=n+1
        for i in range(1, n+2):

           for space in range(1, spaces):
               print(" ", end=" ")

           for j in range(1, i):
               print(j, end=" ")

           for k in reversed(range(1, i-1)):
               print(k, end=" ")

           print()
           spaces-=1


if(__name__=="__main__"):
    obj=Stars()
    obj.printData(9)

Write a program to print following    
           1
                1 2 1
              1 2 3 2 1
            1 2 3 4 3 2 1
          1 2 3 4 5 4 3 2 1
        1 2 3 4 5 6 5 4 3 2 1
      1 2 3 4 5 6 7 6 5 4 3 2 1
    1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
  1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
    1 2 3 4 5 6 7 8
      1 2 3 4 5 6 7
        1 2 3 4 5 6
          1 2 3 4 5
            1 2 3 4
              1 2 3
                1 2
                  1

class Stars:
    def printData(self, n):

        # print first Half
        spaces=n+1
        max_num = n
        for i in range(1, n+2):

           for space in range(1, spaces):
               print(" ", end=" ")

           for j in range(1, i):
               print(j, end=" ")

           for k in reversed(range(1, i-1)):
               print(k, end=" ")

           print()
           spaces-=1

        #Print Second Half
        spaces=0
        for m in range(1, n):
             for p in range(0, spaces):
                 print(" ", end=" ")
             for n in range(1, max_num):
                 print(n, end=" ")
             print()
             max_num-=1
             spaces+=1


if(__name__=="__main__"):
    obj=Stars()
    obj.printData(9) 

Write a program to print following    
                1 
              1 2 1 
            1 2 3 2 1 
          1 2 3 4 3 2 1 
        1 2 3 4 5 4 3 2 1 
      1 2 3 4 5 6 5 4 3 2 1 
    1 2 3 4 5 6 7 6 5 4 3 2 1 
  1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 
  1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 
    1 2 3 4 5 6 7 6 5 4 3 2 1 
      1 2 3 4 5 6 5 4 3 2 1 
        1 2 3 4 5 4 3 2 1 
          1 2 3 4 3 2 1 
            1 2 3 2 1 
              1 2 1 
                1

class Stars:
    def printData(self, n):

        # print first Half
        spaces=n+1
        max_num = n
        for i in range(1, n+2):

           for space in range(1, spaces):
               print(" ", end=" ")

           for j in range(1, i):
               print(j, end=" ")

           for k in reversed(range(1, i-1)):
               print(k, end=" ")

           print()
           spaces-=1

        #Print Second Half
        spaces=1
        for m in range(1, n):
             for p in range(0, spaces):
                 print(" ", end=" ")

             for n in range(1, max_num):
                 print(n, end=" ")

             for q in reversed(range(1, max_num-1)):
                print(q, end=" ")

             print()
             max_num-=1
             spaces+=1

if(__name__=="__main__"):
    obj=Stars()
    obj.printData(9)








Previous                                                 Next                                                 Home

No comments:

Post a Comment