Answer:
1.
num1 = input("Enter the value of n")
n = int(num1)
def calc(n):
Lst = []
while n > 0:
remainder = n % 10
Lst.append(remainder)
quotient = int(n / 10)
n = quotient
i = len(Lst) - 1
sum = 1
for i in range(0, len(Lst)):
if(i % 2 != 0):
sum *=Lst[i];
else:
continue
print(sum)
return(0)
\ r2
num=input("Enter the value of n")
arr=[12,-12,13,15,-34,-35,35,42]
def div7positive():
sum = 0
m = 0
i = 0
while m <= 7:
if(arr[i]>=0 and arr[i]%7 == 0):
sum +=arr[i]
i = i + 1
m += 1
print("sum of number divisible by 7 and positive is", +sum);
div7positive()
\ r
\ r3.Write an algorithm that reads a natural number n and calculates the sum:
\ rS = 1 / (1 * 2) + 1 / (2 * 3) + 1 / (3 * 4) +… + 1 / ((n-1) * n)
\ r
num=input("Enter the value of n")
def sumseries():
sum = 0
m= 0
while m <= 7:
sum +=1/((int(num)-1)*int(num))
m += 1
print("sum of series is", +sum)
sumseries()
\ R4. Read a natural number n. Calculate the sum of its own divisors n. For example, for n = 12, the sum of its own divisors is 2 + 3 + 4 + 6 = 15
num=input("Enter the value of n")
def sumdivisors():
sum = 0
m= 2
while m <= int(num):
if int(num) % m == 0:
sum += m
m += 1
print("sum of divisors is", +sum)
sumdivisors()\ r
\ R5. We read a natural number n and then whole numbers. Calculate and display the sum of the natural numbers between 10 and 100. For example, if n = 5 and then read 30, –2, 14, 200, 122, then the sum will be 44 (that is, 30 + 14).
\ r
Lst = []
num=input("Enter the value of n")
i = 0
while i<= int(num):
num1=input("Enter the element of array")
Lst.append(int(num1));
i += 1
def numbet0and100():
sum = 0
m= 0
while m <= int(num):
if Lst[m] <= 100 or Lst[m] >=0:
sum += Lst[m]
m += 1
print("sum of numbers between 0 and 100 is", +sum)
numbet0and100()
\ R6. A natural number n of maximum 4 digits is read. How many digits are in all numbers from 1 to n? For example, for n = 14 there are 19 digits, and for n = 9 there are 9 digits.
\ r
num = input("Enter the value of n")
n = int(num)
print(“sum as required is:” (n -9) + n)
\ R7. Read the natural numbers n and S, where n can be 2, 3, 4 or 5. Show all the numbers of n digits that have the numbers in strictly ascending order, and the sum of the digits is S. For example, for n = 2 and S = 10, 19, 28, 37, 46 will be displayed.
num = input("Enter the value of n")
n = int(num)
Lst = []
def calc(n):
i = 1
total = pow(10,n)
while i <= total:
j = i
while j > 0:
remainder = j % 10
Lst.append(remainder)
quotient = int(j / 10)
if quotient > 0:
j = quotient
else:
length = len(Lst) - 1
sum = 0
while length >= 0:
sum += Lst[length]
length = length - 1
if sum == pow(10,n):
print(j)
k = len(Lst)
del Lst[0:k]
i = i + 1
return(0)
calc(n)
\ r
\ R8. We consider the row 1, 1, 2, 3, 5, 8, 13, ... in which the first two terms are 1, and any other term is obtained from the sum of the preceding two. Write an algorithm that reads a natural number n and displays the first n terms of this string. For example, for n = 6, 1, 1, 2, 3, 5, 8 will be displayed.
\ r
nterm = int(input("What number of terms do you want?"))
a, b = 0, 1
totalcount = 0
if nterm <= 0:
print("Please input a positive number")
elif nterm == 1:
print("Fibonacci number upto which",nterm,":")
print(a)
else:
print("Fibonacci series:")
print(nterm)
while totalcount < nterm:
print(a)
nth = a + b
a = b
b = nth
totalcount += 1
\ 9. Write an algorithm that reads two natural numbers n1 and n2 and displays the message "yes" if the sum of the squares of the digits of n1 is equal to the sum of the numbers of n2 or "no" otherwise. For example, for n1 = 232 and n2 = 881, "yes" will be displayed, and for n1 = 45 and n2 = 12, "no" will be displayed.
num1 = input("Enter the value of n")
num3 = input("Enter the value of n")
n = int(num1)
num2 = int(num3)
def calc(n):
Lst = []
while n > 0:
remainder = n % 10
Lst.append(remainder)
quotient = int(n / 10)
n = quotient
i = len(Lst) - 1
sum = 0
while i >= 0:
sum += Lst[i]* Lst[i]
i = i - 1
return(sum)
def calc1(num2):
Lst = []
while num2 > 0:
remainder = num2 % 10
Lst.append(remainder)
quotient = int(num2 / 10)
num2 = quotient
i = len(Lst) - 1
sum = 0
while i >= 0:
sum = Lst[i] + Lst[i]
i = i - 1
return(sum)
a = calc(n)
b = calc1(num2)
if a == b:
print("yes")
else:
print("No")
\ r
\ R10. Write an algorithm that reads a natural number n and displays the message "yes" if all of its n numbers are distinct, or "no" if n does not have all the distinct digits. For example, for n = 37645 it will display "yes" and for 23414 it will show "no".
\ r
num1 = input("Enter the value of n")
n = int(num1)
def calc(n):
Lst = []
while n > 0:
remainder = n % 10
Lst.append(remainder)
print( remainder)
quotient = int(n / 10)
n = quotient
flag = 1
for i in range(0, len(Lst)):
for j in range(i+1, len(Lst)):
if Lst[i] == Lst[j]:
flag = 0
break
if flag == 1:
print("YES")
else:
print("NO")
return(0)
Explanation:
Please check answer.