Chapter 6: Loop Control Structures in C

Example 1, Page Number : LCS-2

In [2]:
import sys

n = int(raw_input("\nEnter value to n : "))

for i in range(1,n+1):
    sys.stdout.write("%8d"%(i))
Enter value to n : 15
       1       2       3       4       5       6       7       8       9      10      11      12      13      14      15

Example 2, Page Number : LCS-3

In [8]:
import sys
import numpy
import math

print "\n--------------------------------"
print "          X             Y          "
print "--------------------------------"

for x in numpy.arange(1.0,3.0+0.2,0.2):
    y = 1.36*math.sqrt(1+x+x*x*x)+ pow(x,1.0/4) + math.exp(x)
    sys.stdout.write("\n       %6.2f      %6.2f"%(x,y))
    
print "\n--------------------------------"
--------------------------------
          X             Y          
--------------------------------

         1.00        6.07
         1.20        7.06
         1.40        8.23
         1.60        9.60
         1.80       11.20
         2.00       13.09
         2.20       15.30
         2.40       17.91
         2.60       20.99
         2.80       24.64
         3.00       28.97
--------------------------------

Example 3, Page Number : LCS-4

In [9]:
k = int(raw_input("\nEnter the number : "))

kfact = 1

for i in range(1,k+1):
    kfact = kfact * i
    
print k," factorial is ",kfact
Enter the number : 4
4  factorial is  24

Example 4, Page Number : LCS-6

In [10]:
n = int(raw_input("\nEnter value to N : "))

s = 0

for i in range(1,n+1):
    term = 0
    for j in range(1,i+1):
        term = term + j
    s = s + term
    
print "\nSum of the series S = ",s
Enter value to N : 4

Sum of the series S =  20

Example 5, Page Number : LCS-7

In [11]:
import numpy
import sys

for x in numpy.arange(-1.5,1.5+0.5,0.5):
    for y in numpy.arange(0,3.0+1.0,1.0):
        z = 3*x*x + 2*y*y*y - 25.5
        sys.stdout.write("\n\nValue of y(%0.2f,%0.2f) = %6.2f"%(x,y,z))

Value of y(-1.50,0.00) = -18.75

Value of y(-1.50,1.00) = -16.75

Value of y(-1.50,2.00) =  -2.75

Value of y(-1.50,3.00) =  35.25

Value of y(-1.00,0.00) = -22.50

Value of y(-1.00,1.00) = -20.50

Value of y(-1.00,2.00) =  -6.50

Value of y(-1.00,3.00) =  31.50

Value of y(-0.50,0.00) = -24.75

Value of y(-0.50,1.00) = -22.75

Value of y(-0.50,2.00) =  -8.75

Value of y(-0.50,3.00) =  29.25

Value of y(0.00,0.00) = -25.50

Value of y(0.00,1.00) = -23.50

Value of y(0.00,2.00) =  -9.50

Value of y(0.00,3.00) =  28.50

Value of y(0.50,0.00) = -24.75

Value of y(0.50,1.00) = -22.75

Value of y(0.50,2.00) =  -8.75

Value of y(0.50,3.00) =  29.25

Value of y(1.00,0.00) = -22.50

Value of y(1.00,1.00) = -20.50

Value of y(1.00,2.00) =  -6.50

Value of y(1.00,3.00) =  31.50

Value of y(1.50,0.00) = -18.75

Value of y(1.50,1.00) = -16.75

Value of y(1.50,2.00) =  -2.75

Value of y(1.50,3.00) =  35.25

Example 6, Page Number : LCS-12

In [13]:
n = int(raw_input("\nEnter value to N : "))

s = 0
i = 1

while i <= n:
    s = s + i
    i = i + 2
    
print "\nSum of odd integers = ",s
Enter value to N : 10

Sum of odd integers =  25

Example 7, Page Number : LCS-13

In [15]:
import sys

print "\nInteger divisible by 7"

n = 7

for i in range(1,50+1):
    sys.stdout.write("%8d"%(n))
    n = n + 7
Integer divisible by 7
       7      14      21      28      35      42      49      56      63      70      77      84      91      98     105     112     119     126     133     140     147     154     161     168     175     182     189     196     203     210     217     224     231     238     245     252     259     266     273     280     287     294     301     308     315     322     329     336     343     350

Example 8, Page Number : LCS-14

In [16]:
import sys

n = int(raw_input("\nEnter the end value N : "))

print "\nIntegers not divisible by 7"

for k in range(1,n+1):
    r = k % 7
    if r != 0:
        sys.stdout.write("%8d"%(k))
        
Enter the end value N : 20

Integers not divisible by 7
       1       2       3       4       5       6       8       9      10      11      12      13      15      16      17      18      19      20

Example 9, Page Number : LCS-15

In [17]:
n = int(raw_input("\nEnter a positive integer : "))

q = n
s = 0

while q > 0:
    r = q % 10
    s = s + r
    q = q/ 10
    
print "\nSum of digits = ",s
Enter a positive integer : 2466

Sum of digits =  18

Example 10, Page Number : LCS-16

In [18]:
n = int(raw_input("\nEnter an integer number : "))

q = n
s = 0
while q > 0:
    r = q % 10
    s = s + r*r*r
    q = q/10
    
if n == s:
    print "\n",n," is an Armstrong number"
else:
    print "\n",n," is not an Armstrong number"
Enter an integer number : 153

153  is an Armstrong number

Example 11, Page Number : LCS-17

In [19]:
n = int(raw_input("\nEnter an integer number : "))

q = n
rn = 0

while q > 0:
    r = q % 10
    rn = rn*10 + r
    q = q/10
    
print "\n",n," is reversed as ",rn
Enter an integer number : 18532

18532  is reversed as  23581

Example 12, Page Number : LCS-18

In [20]:
n = int(raw_input("\nEnter an integer number : "))

q = n
rn = 0

while q > 0:
    r = q % 10
    rn = rn*10 + r
    q = q/10

while rn > 0:
    r = rn % 10
    
    #There is no switch statement in python
    if r == 1:
        print "One"
    else:
        if r == 2:
            print "Two"
        else:
            if r == 3:
                print "Three"
            else:
                if r == 4:
                    print "Four"
                else:
                    if r == 5:
                        print "Five"
                    else:
                        if r == 6:
                            print "Six"
                        else:
                            if r == 7:
                                print "Seven"
                            else:
                                if r == 8:
                                    print "Eight"
                                else:
                                    if r == 9:
                                        print "Nine"
                                    else:
                                        print "Zero"
        rn = rn/10
Enter an integer number : 4352
Four
Three
Five
Two

Example 13, Page Number : LCS-20

In [21]:
flag = 0

n = int(raw_input("\nEnter a positive integer : "))

for k in range(2,n/2+1):
    if flag == 0:
        r = n % k
        if r == 0:
            flag = 1
            
if flag == 0:
    print "\n",n," is a prime number"
else:
    print "\n",n," is not a prime number"
Enter a positive integer : 17

17  is a prime number

Example 14, Page Number : LCS-21

In [22]:
import sys

n = int(raw_input("\nEnter the final term of the series : "))

n1 = 0
n2 = 1

sys.stdout.write("%8d%8d"%(n1,n2))

newterm = n1 + n2

while newterm <= n:
    sys.stdout.write("%8d"%(newterm))
    n1 = n2
    n2 = newterm
    newterm = n1 + n2
    
Enter the final term of the series : 25
       0       1       1       2       3       5       8      13      21

Example 15, Page Number : LCS-22

In [23]:
x = float(raw_input("\nEnter x in radians : "))
n = int(raw_input("\nEnter end term power (n) : "))

s = 0
term = x
i = 1

while i <= n:
    s = s + term
    term = (term*x*x*(-1))/((i+1)*(i+2))
    i = i + 2
    
print "\nSum of the series = ",s
Enter x in radians : 0.52

Enter end term power (n) : 10

Sum of the series =  0.496880137863

Example 16, Page Number : LCS-24

In [24]:
x = float(raw_input("\nEnter x in radians : "))

s = 0
term = 1
i = 0

while i <= n:
    s = s + term
    term = (term*x*x*(-1))/((i+1)*(i+2))
    i = i + 2
    
print "\nSum of the series = ",s
Enter x in radians : 0.52

Sum of the series =  0.867819179677

Example 17, Page Number : LCS-25

In [27]:
import sys
import math

s = 0
dr = 1
sign = 1
term = 1.0/dr * sign

while (math.fabs(term) >= 1.0e-4):
    s = s + term
    dr = dr + 2
    sign = sign * (-1)
    term = (1.0/dr) * sign
    
pie = s * 4

sys.stdout.write("\nValue of pie is %0.2f"%(pie))
Value of pie is 3.14

Example 18, Page Number : LCS-27

In [29]:
import sys

n = int(raw_input("\nEnter value to N : "))

s = 0

for i in range(1,n+1):
    s = s + 1.0/i
    
sys.stdout.write("\nSum of series = %8.4f"%(s))
Enter value to N : 15

Sum of series =   3.3182

Example 19, Page Number : LCS-28

In [35]:
import sys

n = int(raw_input("\nHow many lines ? "))

for l in range(1,n+1):
    for i in range(1,n-l+1):
        sys.stdout.write("      ")
        
    m = l
    for j in range(1,l+1):
        sys.stdout.write("%6d"%(m))
        m = m + 1
    m = m - 2
    
    for k in range(1,l):
        sys.stdout.write("%6d"%(m))
        m = m - 1
        
    sys.stdout.write("\n")
    
How many lines ? 5
                             1
                       2     3     2
                 3     4     5     4     3
           4     5     6     7     6     5     4
     5     6     7     8     9     8     7     6     5

Example 20, Page Number : LCS-29

In [38]:
import sys

n = int(raw_input("\nHow many rows ? "))

print "\n\t\t\t\t PASCAL TRIANGLE\n"

m = 1

for l in range(0,n):
    for i in range(40-3*l,0,-1):
        sys.stdout.write(" ")
        
    for j in range(0,l+1):
        if j == 0 or l == 0:
            m = 1
        else:
            m = (m*(l-j+1))/j
        sys.stdout.write("%6d"%(m))
    sys.stdout.write("\n")
How many rows ? 6

				 PASCAL TRIANGLE

                                             1
                                          1     1
                                       1     2     1
                                    1     3     3     1
                                 1     4     6     4     1
                              1     5    10    10     5     1

Example 21, Page Number : LCS-31

In [39]:
import sys

for i in range(1,5+1):
    sys.stdout.write("\n\nMultiplication table for %d\n"%(i))
    
    for j in range(1,10+1):
        sys.stdout.write("\n%4d x %2d = %4d"%(j,i,j*i))
    
    sys.stdout.write("\n\nPress any key to continue...")

Multiplication table for 1

   1 x  1 =    1
   2 x  1 =    2
   3 x  1 =    3
   4 x  1 =    4
   5 x  1 =    5
   6 x  1 =    6
   7 x  1 =    7
   8 x  1 =    8
   9 x  1 =    9
  10 x  1 =   10

Press any key to continue...

Multiplication table for 2

   1 x  2 =    2
   2 x  2 =    4
   3 x  2 =    6
   4 x  2 =    8
   5 x  2 =   10
   6 x  2 =   12
   7 x  2 =   14
   8 x  2 =   16
   9 x  2 =   18
  10 x  2 =   20

Press any key to continue...

Multiplication table for 3

   1 x  3 =    3
   2 x  3 =    6
   3 x  3 =    9
   4 x  3 =   12
   5 x  3 =   15
   6 x  3 =   18
   7 x  3 =   21
   8 x  3 =   24
   9 x  3 =   27
  10 x  3 =   30

Press any key to continue...

Multiplication table for 4

   1 x  4 =    4
   2 x  4 =    8
   3 x  4 =   12
   4 x  4 =   16
   5 x  4 =   20
   6 x  4 =   24
   7 x  4 =   28
   8 x  4 =   32
   9 x  4 =   36
  10 x  4 =   40

Press any key to continue...

Multiplication table for 5

   1 x  5 =    5
   2 x  5 =   10
   3 x  5 =   15
   4 x  5 =   20
   5 x  5 =   25
   6 x  5 =   30
   7 x  5 =   35
   8 x  5 =   40
   9 x  5 =   45
  10 x  5 =   50

Press any key to continue...

Example 22, Page Number : LCS-32

In [40]:
n = int(raw_input("\nEnter the binary number : "))

q = n
s = 0
k = 0

while q > 0:
    r = q % 10
    s = s + r * pow(2,k)
    q = q/10
    k = k + 1
    
print "\nThe decimal number is : ",s
Enter the binary number : 1101

The decimal number is :  13

Example 13, Page Number : LCS-34

In [41]:
flag = 0
k = 0

n = int(raw_input("\nEnter the decimal number : "))

q = n
rbi = 0

while q > 0:
    r = q % 2
    if r == 0 and flag == 0:
        k = k + 1
    else:
        flag = 1
    rbi = rbi *10 + r
    q = q/2
    
q = rbi
bi = 0

while q > 0:
    r = q % 10
    bi = bi * 10 + r
    q = q/10
    if q == 0:
        for i in range(1,k+1):
            bi = bi * 10
            
print "\nThe binary number is ",bi
Enter the decimal number : 28

The binary number is  11100
In [ ]: