Programs 1
Programs 1
# printing values
print("Sum of", num1, "and", num2 , "is", sum)
num1 = 15
num2 = 12
# printing values
print("Sum of {0} and {1} is {2}" .format(num1,
num2, su))
num = 29
if num == 1:
print(num, "is not a prime number")
elif num > 1:
# check for factors
for i in range(2, num):
if (num % i) == 0:
# if factor is found, set flag to True
flag = True
# break out of loop
break
num = 407
if num == 1:
print(num, "is not a prime number")
elif num > 1:
# check for factors
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
print(i,"times",num//i,"is",num)
break
else:
print(num,"is a prime number")
num = 320
Output
print_factors(num)
The factors of 320 are:
1
2
4
5
8
10
16
20
32
40
64
80
160
320
lwr = 100
uppr = 2000
# order of number
odr = len(str(numb))
# initialize sum
sum = 0
tem = numb
digit = tem % 10
sum += digit ** odr
tem //= 10
if numb == sum:
153
370
371
407
1634
fahren = float(input())
celi = (fahren-32)/1.8
Output:
98
import math
n = int (input (“Enter the number whose factorial you want to find: ”))
14) Write a program to calculate the factorial of a number in Python using FOR
loop.
Copy Code
factorial = 1
if n >= 1:
factorial = factorial *i
15) Write the Python program to calculate the factorial of a number using
recursion.
Copy Code
n = int (input (“Enter the number for which the factorial needs to be calculated: “)
if n == 1:
return n
elif n < 1:
else:
if n < 0:
else:
sum = 0
sum +=n
n -=1
Output:
Enter any natural number:
100
if n < 0:
else:
sum = n * (n+1) / 2
‘a’
‘T’
19) Python Program to Convert Decimal to Binary Using Recursion
def convertToBin(a):
if a > 1:
convertToBin(a//2)
# decimal number
d = 34
convertToBin(d)
print()
Output
100010
Step 11: Since number becomes = 0. Now we consider the remainders in the reversed
order. Therefore the equivalent binary number is 10001.
Source Code:
def FibRec(a):
if a <= 1:
return a
else:
return(FibRec(a-1) + FibRec(a-2))
if aterms <= 0:
else:
print("Fibonacci sequence:")
for z in range(aterms):
print(FibRec(z))
Output:
5
01123
Source Code:
first=0
second=1
if i<=0:
",first)
else:
print(first,second,end=" ")
for x in range(2,i):
n=first+second
print(n,end=" ")
first=second
second=n
1
2
# The term "casefold" has been used to refer to a method of ignoring cases.
string = string.casefold()
count = {}.fromkeys(vowels, 0)
if character in count:
count[character] += 1
return count
# Driver Code
vowels = 'aeiou'
Output:
print("Vowel")
else:
print("Consonant")
Output:
Vowel
print("Vowel")
else:
print("Consonant")
Output:
Consonant
for a in range(1,11):
print(num,'x',a,'=',num*a)
Output
Multiplication Table of :
7x1=7
7 x 2 = 14