So I’m trying to write a program to find the sum of digits of 2 to the power 100 using python, and this is my code so far.
import math
def first_digit(n):
while n >= 10: # Change ‘>’ to ‘>=’ to correctly get the first digit
n //= 10
return n
def sumofdigits(num):
sum = 0
dig = [] # Reset the list inside the function
for i in range(101, 0, -1):
real = str(num)
for j in range(1, len(real)+1):
dig.append(first_digit(num%(10**j)))
sum=sum+dig[j-1]
x = pow(2,100)
print(sumofdigits(x))
I tried running this code but it doesn’t return anything. What is going wrong here?
Akul is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.