1. Write a program to print numbers in reverse order with a difference of 2, set the initial value to 10.
2. Write a program to print the sum of digits of any number entered by the user.
3. Write a program to find the product of digits to any number entered by user.
4. Write a program to find the factorial of any number.
5. Write a program to convert a binary number to a decimal number.
num = 10
i = 2
while num >= 0:
print(num)
num = num - i
def getSum(n):
sum = 0
for digit in str(n):
sum += int(digit)
return sum
n = 12345
print(getSum(n))
def getProduct(n):
product = 1
while (n != 0):
product = product * (n % 10)
n = n // 10
return product
n = 4513
print(getProduct(n))
# Python program to find the factorial of a number provided by the user.
# change the value for a different result
num = int(input('Enter integer here: '))
# To take input from the user
#num = int(input("Enter a number: "))
factorial = 1
# check if the number is negative, positive or zero
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
b_num = list(input("Input a binary number: "))
value = 0
for i in range(len(b_num)):
digit = b_num.pop()
if digit == '1':
value = value + pow(2, i)
print("The decimal value of the number is", value)
Comments
Leave a comment