You are on page 1of 4

ASSIGNMENT I

(Last Day of Submission on 20th Oct 2015)

1. Python programme to convert decimal number to binary numbers.


a. Without using recursive function
Soln

b. Using recursive function


Programme :

i=1
s=0
dec=input("Enter decimal to be converted: ")
while dec>0:
rem=dec%2
s=s+(i*rem)
dec=dec/2
i=i*10
print "The binary of the given number is ",s,'.'
raw_input()

Output:

Enter decimal to be converted: 123


The binary of the given number is 1111011 .

2. Write a python program to reverse the digits of an integer


a. without using recursive function
Soln:

print 'This program accepts a number and then reverses it'


number = int(raw_input("Enter a Integer = "))
temp_var = 0
while (number/10) > 1 :
temp_var = (temp_var*10) + (number%10)
number = number/10
else:
temp_var = (temp_var*10) + number
print 'The reversed number is ', temp_var

Output

This program accepts a number and then reverses it

Enter a number = 234


The reversed number is 432
b. using recursive function
Soln

3. Write a python program to find the Greatest common divisor(GCD) of two numbers.
4. Write a python programme to verify a given number is prime or not

5. Write a prpograme to generate prime number between 1 to 100


Program :
def is_prime(n):
status = True
if n < 2:
status = False
else:
for i in range(2,n):
if n % i == 0:
status = False
return status
for n in range(1,101):
if is_prime(n):
if n==97:
print n
else:
print n,",",

Output :
2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 , 53 , 59 , 61 , 67 ,
71 , 73 , 79 , 83 , 89 , 97

6. Write a python program to organize a given set of numbers in ascending order using
a. Insertion sort
b. Bubble sort
c. Selection sort
7. Write a python program to find out the Prime number from a sequence of Fibonacci
series.
Program:

def fib(n):
cur = 1
old = 1
i=1
while (i < n):
cur, old, i = cur+old, cur, i+1
return cur
for i in range(10):
print(fib(i))

8. Python program to check a given number is an Armstrong number or not


Program:​
num = int(input("Enter a number: "))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")

Output :

Enter a number: 371


(371, 'is an Armstrong number')

You might also like