Chapter 05: Loop Control Structures in C

Example 1 , Page number: CP-79

In [1]:
# Program to print natural numbers from 1 to n
# Variable decalration

n = 15

print "Enter value to n :",n

# Loop to print natural numbers
for r in range(15):
    r = r + 1
    print r,
    
    
Enter value to n : 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Example 2 , Page number: CP-80

In [2]:
# Program to find the value of y and print a table for values of x
# Variable declaration
import math
x = 1.0
print "------------------------"
print "     x          y       "
print "------------------------"
while x <= 3.2:
    y = 1.36 * ((1 + x + x * x * x) ** 0.5) + ((x) ** (1.0/4)) + math.exp(x)
    print "     %0.2f   " %x  + "   %0.2f"  % y
    x = x + 0.2
------------------------
     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: CP-81

In [2]:
# Program to find factorial of given number
# Variable declaration

k = 4
kfact = 1

print "Enter an integer :",k

# Loop to generate numbers from 1 to n

for i in range(1,k + 1):
       kfact = kfact*i

print "4 factorial is",kfact    
Enter an integer : 4
4 factorial is 24

Example 4 , Page number: CP-83

In [4]:
# Program to print sum of the following series
# Variable declaration

n = 4
s = 0

print "Enter value to N :",n
# Calculation of sum
i = 1
j = 1

for i in range(1,n+1):
    term = 0
    for j in range(i+1):
        term = term + j
    s = s + term


print "Sum of the series S =",s    
Enter value to N : 4
Sum of the series S = 20

Example 5 , Page number: CP-84

In [5]:
# Program to compute the values of z based on x and y
# Variable declaration

x = -1.5
y = 0

# Loops to generate values of x and y to find z

while x <= 1.5:
    while y <= 3.0:
        z = 3 * x * x + 2 * y * y * y - 25.5
        print "Value of y(",x,
        print ",",y,
        print ") =",z
        y = y + 1.0
    x = x + 0.5
    y = 0
Value of y( -1.5 , 0 ) = -18.75
Value of y( -1.5 , 1.0 ) = -16.75
Value of y( -1.5 , 2.0 ) = -2.75
Value of y( -1.5 , 3.0 ) = 35.25
Value of y( -1.0 , 0 ) = -22.5
Value of y( -1.0 , 1.0 ) = -20.5
Value of y( -1.0 , 2.0 ) = -6.5
Value of y( -1.0 , 3.0 ) = 31.5
Value of y( -0.5 , 0 ) = -24.75
Value of y( -0.5 , 1.0 ) = -22.75
Value of y( -0.5 , 2.0 ) = -8.75
Value of y( -0.5 , 3.0 ) = 29.25
Value of y( 0.0 , 0 ) = -25.5
Value of y( 0.0 , 1.0 ) = -23.5
Value of y( 0.0 , 2.0 ) = -9.5
Value of y( 0.0 , 3.0 ) = 28.5
Value of y( 0.5 , 0 ) = -24.75
Value of y( 0.5 , 1.0 ) = -22.75
Value of y( 0.5 , 2.0 ) = -8.75
Value of y( 0.5 , 3.0 ) = 29.25
Value of y( 1.0 , 0 ) = -22.5
Value of y( 1.0 , 1.0 ) = -20.5
Value of y( 1.0 , 2.0 ) = -6.5
Value of y( 1.0 , 3.0 ) = 31.5
Value of y( 1.5 , 0 ) = -18.75
Value of y( 1.5 , 1.0 ) = -16.75
Value of y( 1.5 , 2.0 ) = -2.75
Value of y( 1.5 , 3.0 ) = 35.25

Example 6 , Page number: CP-89

In [6]:
# Program to find sum of odd integers between 1 to n
# Variable declararion

n = 10

print "Enter value to N :",n

s = 0
i = 1

while (i <= n):
    s = s + i
    i = i + 2

print "Sum of odd integers =",s
Enter value to N : 10
Sum of odd integers = 25

Example 7 , Page number: CP-90

In [7]:
# Program to generate first 50 positive integers that are divisible by 7
# Variable declaration

n = 7
print "Integers divisible by 7"

#loop to print numbers divisible by 7
for n in range(7,353,7):
    print n,
    

    
Integers 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: CP-91

In [8]:
# Program to print integers from 1 to n that are not divisible by 7
# Variable declaration

n = 20
print "Enter the end value N :",n

# Loop to print numbers that are not divisible by 7

print "Integers not divisible by 7"
for k in range(1,n + 1,1):
    r = k % 7
    if (r != 0):
        print 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: CP-92

In [9]:
# Program to print sum of digits of an integer
# Variable declaration

n = 2466

print "Enter a positive integer :",n

q = n
s = 0

while (q > 0):
    r = q % 10
    s = s + r
    q = q / 10


print "Sum of digits =",s
Enter a positive integer : 2466
Sum of digits = 18

Example 10 , Page number: CP-93

In [10]:
# Program to check whether a given number is an armstrong number or not
# Variable declaration

n = 153
q = n
s = 0

print "Enter an integer number :",n

# To check armstrong or not

while (q > 0):
    r = q % 10
    s = s + r * r * r
    q = q / 10

if (n == s):
    print "%d is an Armstrong number" % n
else:
    print "%d is not an Armstrong number" % n


        
Enter an integer number : 153
153 is an Armstrong number

Example 11 , Page number: CP-94

In [11]:
# Program to reverse a given integer
# Variable declaration

n = 18532
q = n
rn = 0

print "Enter an integer number :",n
# Reversing

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


print "18532 is reversed as",rn   
Enter an integer number : 18532
18532 is reversed as 23581

Example 12 , Page number: CP-95

In [12]:
# Program to accept an integer and print digits using words
# Variable declaration

n = 4352
q = n
rn = 0

print "Enter an integer number :",n
# converting to digits

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


while (rn > 0):
    r = rn % 10
    if (r == 1):
        s = "One"
        print s,
    elif (r == 2):
        s = "Two"
        print s,
    elif (r == 3):
        s = "Three"
        print s,
    elif (r == 4):
        s = "Four"
        print s,
    elif (r == 5):
        s = "Five"
        print s,
    elif (r == 6):
        s = "Six"
        print s,
    elif (r == 7):
        s = "Seven"
        print s,
    elif (r == 8):
        s = "Eight"
        print s,
    elif (r == 9):
        s = "Nine"
        print s,
    elif (r == 0):
        s = "Zero"
        print s,
    rn = rn / 10



    
Enter an integer number : 4352
Four Three Five Two

Example 13 , Page number: CP-97

In [1]:
# Program to find whether a number is prime or not
# Variable declaration

n = 17

print "Enter a positive integer :",n

# prime numbers are greater than 1
if n > 1:
    for i in range(2,n):
        if (n % i) == 0:
            print(n,"is not a prime number")
            print(i,"times",n//i,"is",num)
            break
    else:
        print "%d is a prime number" % n

else:
    print "%d is not a prime number" % n
Enter a positive integer : 17
17 is a prime number

Example 14 , Page number: CP-98

In [14]:
# Program to generate Fibonacci series
# Variable declaration

n = 25
n1 = 0
n2 = 1

print "Enter the final term of the series :",n

print n1,n2,


newterm = n1 + n2

# Loop to print fibonacci series
while (newterm <= n):
    print 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: CP-99

In [15]:
# Program to solve the sine  series
# Variable declaration

x = 0.52
n = 10
s = 0
term = x
i = 1

print "Enter x in radians :",x
print "Enter end term power (n):",n

while (i <= n):
    s = s + term
    term = (term * x * x *(-1)) / ((i + 1) * (i + 2))
    i = i + 2

print "Sum of the series = %0.6f" % s
Enter x in radians : 0.52
Enter end term power (n): 10
Sum of the series = 0.496880

Example 16 , Page number: CP-101

In [16]:
# Program to  solve the cosine series
# Variable declaration

x = 0.52
s = 0
term = 1
i = 0
n = 10
print "Enter x in radians :",x

# Calculation of cosine series

while (i <= n):
    s = s + term
    term = (term * x * x * (-1)) / ((i + 1) * (i + 2))
    i = i + 2


print "Sum of the series = %0.6f" % s    
Enter x in radians : 0.52
Sum of the series = 0.867819

Example 17 , Page number: CP-102

In [17]:
import math

# Program to compute the value of pie
# Variable declaration

s = 0.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

print "Value of pie is %.6f"%pie
Value of pie is 3.141393

Example 18 , Page number: CP-104

In [18]:
# Program to evaluate the series
# Variable declaration

n = 15
s = 0.00

print "Enter value to N :",n

# Evaluation of series

for i in range(1,n + 1,1):
    s = float(s) + float(1.0 / i)
    

print "Sum of series = %0.4f" % s    
    
Enter value to N : 15
Sum of series = 3.3182

Example 21 , Page number: CP-108

In [19]:
# Program to print multiplication table
# Variable declaration

i = 1
j = 1

# nested loop to print multiplication tables

for i in range(1,6):
    print "Multiplication table for",i
    for j in range(1,11):
        print " ",j,"x" , i ," =" , j*i
    print "Press 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: CP-109

In [20]:
import math

# Program to convert a binary number to a decimal number
# Variable declaration

q = 1101
s = 0
k = 0

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


print "The decimal number is :",s
Enter the binary number : 1101
The decimal number is : 13

Example 23 , Page number: CP-110

In [3]:
# Program to convert a decimal number to a binary number
# Variable declaration

n = 28
q = n
rbi = 0
flag = 0
k = 0

print "Enter the decimal number :",n


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
    i = 1
    if (q == 0):
        while (i <= k):
            bi = bi * 10
            i = i + 1


print "The binary number is ",bi            
Enter the decimal number : 28
The binary number is  11100