# Chapter 04 : Branching and Looping¶

## Example 1: Page No.:4.88¶

In :
# addition of numbers using while loop.

#Varibale Declaration

i=1
sum=0
while i<=10:                    #Checking while loop
sum+=i
i+=1                        #Increment of i

#Result
print "The Sum of the numbers upto is ",sum

The Sum of the numbers upto is  55


## Example 2: Page No.: 4.89¶

In :
import sys

#Variable Declaration

i=1
j=0

print "The maximum value of integer is", sys.maxsize
print "The Value of integer after overflow is", sys.maxint-1

 The maximum value of integer is 2147483647
The Value of integer after overflow is 2147483646


## Example 3: Page No.: 4.89¶

In :
#Reverse the given number using while loop

#Variable Declaration

number=digit=rev=0

number=input("Give the Value: ")

while(number!=0):
digit=number%10
rev=rev*10+digit
number= number/10

print "The reversible value of given number is", rev

Give the Value: 7896
The reversible value of given number is 6987


## Example 4: Page No.: 4.90¶

In :
# Simple interest using while loop.

#Variable Declaration

p=1
n=1
count=1

r=si=0.0

while(count<=3):
p=input("Give the value of P" '\t')
n=input("Give the value of N"'\t')
r=float(input("Give the value of R"'\t'))

si="%.3f"%float((p*n*r)/100)

print "Simple Interest of SI", si
count=count+1

Give the value of P	2000
Give the value of N	3
Give the value of R	5
Simple Interest of SI 300.000
Give the value of P	3500
Give the value of N	6
Give the value of R	1.5
Simple Interest of SI 315.000
Give the value of P	6000
Give the value of N	2
Give the value of R	3.5
Simple Interest of SI 420.000


## Example 5: Page No.:4.92¶

In :
# Numbers upto 10 by using the do..while loop

#Variable Declaration

i=1
sum=0

while True:
sum+=i
i+=1
if(i>10):
break
print "Sum of the numbers upto 10 is", sum


Sum of the numbers upto 10 is 55


## Example 6: Page No.: 4.92¶

In :
#print n numbers using do...while loop

i=0

n=input("Enter the number: ")

while True:
print "The numbers are: ",i
i=i+1
if(i>=n):
break

Enter the number: 6
The numbers are:  0
The numbers are:  1
The numbers are:  2
The numbers are:  3
The numbers are:  4
The numbers are:  5


## Example 7: page no.: 4.95¶

In :
#Addition of number upto 10 using for loop

i=sum=0

for i in range (0,11):
sum+=i
i+=1

print "The addition of given numbers",sum


The addition of given numbers 55


## Example 8: Page No.:4.96¶

In :
#print n numbers using for...loop structure

n=int(raw_input("Give Values of n: "))
i=0
for i in range(0,n):
print "The Given numbers are: ",i


Give Values of n: 5
The Given numbers are:  0
The Given numbers are:  1
The Given numbers are:  2
The Given numbers are:  3
The Given numbers are:  4


## Example 9: Page No.:4.97¶

In :
#student marks and average using for loop..

a=b=c=d=e=total=0

avg=0.0

m=input("Enter the number of Students:")

print "Enter the marks of the five subjects: "'\n'

for i in range (m):
i+=1
print "\nStudent:",i,"\n"

a,b,c,d,e=input("Enter the Student mark1: ")

if ((a,b,c,d,e)!=0) :
total=a+b+c+d+e
avg="%.3f"%float(total/5)
print "Total marks of the student:", total
print "Average marks of the students: ",avg

else  :
print "Entered data is invalid"


Enter the number of Students:3
Enter the marks of the five subjects:

Student: 1

Enter the Student mark1: 78,56,90,59,87
Total marks of the student: 370
Average marks of the students:  74.000

Student: 2

Enter the Student mark1: 49,87,60,57,93
Total marks of the student: 346
Average marks of the students:  69.000

Student: 3

Enter the Student mark1: 89,100,80,97,65
Total marks of the student: 431
Average marks of the students:  86.000


## Example 10: Page No.:4.98¶

In :
#Nested for loop

for i in range(1,4):
for j in range(1,4):
print "%d"%(j),
print "\n"

1 2 3

1 2 3

1 2 3



## Example 11: Page No.:4.99¶

In :
#student details using nested for loop..

FIRST=300
SECOND=200
rollno=marks=total=0

n=input("Enter the number of Students: ")
m=input("Enter the number of subjects: ")

for i in range(n):
i+=(+1)
rollno=int(raw_input("Enter the student roll number: "))
total=0
print "Enter marks of",m, "subjects for Roll no: ",rollno
for j in range(m):
marks=int(raw_input("Give marks: "))
total= total+marks

print "total marks", total
if(total>=FIRST):
print "(First Class)"'\n'
elif(total>=SECOND):
print "(Second Class)"'\n'
else:
print "Fail"

Enter the number of Students: 2
Enter the number of subjects: 5
Enter the student roll number: 201
Enter marks of 5 subjects for Roll no:  201
Give marks: 90
Give marks: 78
Give marks: 90
Give marks: 68
Give marks: 89
total marks 415
(First Class)

Enter the student roll number: 202
Enter marks of 5 subjects for Roll no:  202
Give marks: 40
Give marks: 50
Give marks: 49
Give marks: 39
Give marks: 47
total marks 225
(Second Class)



## Example 12: Page No.: 4.103¶

In :
#Demonstrate switch case

a=1
if a==1:
print "I am in case 1"'\n'
elif a==2:
print "I am in case2"'\n'
else:
print "I am in case default"'\n'

I am in case 1



## Example 13: Page No.:4.104¶

In :
#Program to use the computer as calculator

a=b=c=0

op=raw_input("Give the option for calculation like +(ADD), -(SUB), *(MUL), /(DIV): ")
a=input("Enter the value of a ")
b=input("Enter the value of b ")

#Calculation
if (op=='+'):
c=a+b
print "Result of C", c
elif(op=='-'):
c=a-b
print "Result of C", c
elif(op=='*'):
c=a*b
print "Result of C", c
else:
c=a/b
print "Result of C", c

Give the option for calculation like +(ADD), -(SUB), *(MUL), /(DIV): +
Enter the value of a 20
Enter the value of b 10
Result of C 30


## Example 14: Page No.:4.105¶

In :
#Find even or odd numbers

#USer Input
a=input("Enter a number: ")

if a%2==0 :
print  " The number is even. "
else :
print "The number is odd."


Enter a number: 89
The number is odd.


## Example 15: Page No.:4.106¶

In :
#Program to count number of 0s, 1s, blank spaces and other characters

txt=
count =0
txt=raw_input("Enter Numbers")

print "Total Spaces  : %d"%txt.count(' ')
print "Total of 1's  : %d"%txt.count('1')
print "Total of 0's  : %d"%txt.count('0')
print "String Length : %d"%len(txt)


Enter Numbers120010  09100110
Total Spaces  : 2
Total of 1's  : 5
Total of 0's  : 7
String Length : 16


## Example 16: Page No.: 4.108¶

In :
#Print the number upto 5 using break statement

for i in range(1,10):
if i==6:
break
print i,

1 2 3 4 5


## Example 17: Page No.: 4.109¶

In :
#Program to calculate the sum of the given positive numbers

i=n=sum=0

for i in range(0,5):
n=int(raw_input("Give any value of n "))
if n<0:
continue
else:
sum+=n

print "\nSum is", sum

Give any value of n 10
Give any value of n 15
Give any value of n 25
Give any value of n -10
Give any value of n 50

Sum is 100


## Example 18: Page No.:4.113¶

In :
#Program using goto statement

a=input("Give the Value of a ")
b=input("Give the value of b ")

if a==b:
print "A and B are equal"
else:
print "A and B are not equal"
exit(0)

Give the Value of a 3
Give the value of b 3
A and B are equal


## Case Study 1: Page No.: 4.112¶

In :
#Prime or not

n=input("Enter the Positive value of n ")
i=2

#Calculation
while n>i:
if n%i==0 & i!=n:
#r=n%i
#if r==0:
print "The Given value of ",n," is not a prime"
break
i+=1
else:
print "The Given Value",n, "is Prime"


Enter the Positive value of n 3
The Given Value 3 is Prime


## Case Study 2: Page No.: 4.113¶

In :
#Find the Greatest Common Division

i=int(input("Give the Value of i "))
j=int(input("Give the value of j "))

def gcd(i,j):
if i > j:
k = i%j
if k == 0:
return j
else:
return gcd(j, k)
if i < j:
i = j
j = i
return gcd(i, j)
print "The value of gcd", gcd(i,j)

Give the Value of i 54
Give the value of j 36
The value of gcd 18


## Case Study 3: Page No.: 4.115¶

In :
#Program to which reads in a Temperature and print the temperature in opposite scale

print "1.Fahrenheit to Celcius\n2.Celcius to Fahrenheit\n"
code=input("Choose Option to find Temperature: ")

while code!=0:
if code==1:
f=float(input("Give the Value for Fahrenheit Temperature "))
c=(f-32.0)*5.0/9.0
break
elif code==2:
c=float(input("Give the Value for Celcius Temperature "))
f=9.0*c/5.0+32.0
break
else:
print "Give proper Code Value for Temperature Calculation"
break
print "C=", "%.3f"%float(c)
print "F=",f

1.Fahrenheit to Celcius
2.Celcius to Fahrenheit

Choose Option to find Temperature: 1
Give the Value for Fahrenheit Temperature 100
C= 37.778
F= 100.0