Sum of digits in a number [duplicate] - python

This question already has answers here:
How to manage division of huge numbers in Python?
(4 answers)
What is a debugger and how can it help me diagnose problems?
(2 answers)
Closed 2 years ago.
This function is supposed to return the sum of all the digits in a number.
def sum_digits(num):
if int(num) <= 0:
return 0
else:
sum = 0
while int(num) != 0:
n = int(num) % 10
num = int(num) / 10
sum = int(sum + n)
return sum
It works fine for smaller numbers, but if i enter for example number 999999999999999999999999999999999 it returns 74 instead of 297.
Thanks for help

You need to divide with (//) instead of (/), because that would return a floating number.
You need to do something like this:
def sum_digits(num):
num = int(num)
if num <= 0:
return 0
else:
sum = 0
while num != 0:
n = num % 10
num //= 10
sum += n
return sum
(I reformated your code a little for better readability.)
edit: typo fixed

I approached your problem a little different
number = 999999999999999999999999999999999
number = str(number)
sum = 0
for digit in number:
digit = int(digit)
sum = digit+sum
print(sum)

Related

Python - Why does it prints "none"? [duplicate]

This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed last month.
i = 1
input_number = int(input("Input a digit you wish to count: "))
def count(n):
global i
n = int(n/10)
if n > 0:
i = i+1
count(n)
else:
j = i
print(f"j={j}")
return j
j = count(input_number)
print(f"i={i}")
print(j)
I'm trying to use a recursive way to print the digits of a number. I used a global counter to count, and can print the global counter as result. However, my question is - why can't I make the function to return the counter and print the function result directly? It returns None somehow.
The count function does not always return a value, so in those cases, it returns None.
If n is greater than 0, no return is encountered.
if n > 0:
i = i+1
count(n)
You were likely wanting:
if n > 0:
i = i+1
return count(n)
Please also note that if you want integer division, you can use //. E.g. n = int(n/10) can be n = n // 10 or just n //= 10.

Sum of n natural numbers using while loop in python [duplicate]

This question already has answers here:
Sum of the integers from 1 to n
(11 answers)
Closed 6 months ago.
The question was tp :write a program to find the sum of n natural numbers using while loop in python.
n = int(input("Enter a number: "))
i = 1
while i<n:
print(i)
i = i + 1
this is what I have done s far...
can not understand what to do next.
n = int(input("enter a number: "))
i = 1
sum = 0
while (i <= n):
sum = sum + i
i = i + 1
print("The sum is: ", sum)
with a while loop the sum of natural numbers up to num
num = 20
sum_of_numbers = 0
while(num > 0):
sum_of_numbers += num
num -= 1
print("The sum is", sum_of_numbers)
You can either follow Alasgar's answer, or you can define a function with the formula for this particular problem.
The code's gonna be something like this:
def natural(n):
sumOfn = (n * (n + 1))/2
terms = int(input("Enter number of terms: "))
natural(terms)
number = int(int(input("Enter the number: "))
if number < 0:
print("Enter a positive number: ")
else:
totalSum = 0
while (number > 0):
totalSum += number
number -= 1
print ("The sum is" , totalSum)
num = int(input('Enter the number : '))
sum = 0
while 0<num:
sum += num
num -= 1
print(f'The sum of the number is {sum}')

Finding magic numbers in python [duplicate]

This question already has answers here:
Sum the digits of a number
(11 answers)
Closed 2 years ago.
I am trying to find the magic number in this program but I got stuck on this part and am not sure where to go next. I have searched up many ways on the internet but they are all using more complex code that I have not learned yet.
Example
input 45637
4+5+6+3+7 = 25
2+5 = 7
7 = magic number
num = int(input("Enter a positive number : "))
ans = 0
while num > 0 or ans > 9:
digit = num % 10
num = num//10
print(digit)
Using statements and operators you have already learned as demonstrated in your code, you can use a nested while loop to aggregate the digits from the division remainders into a total as the number for the next iteration of the outer while loop:
num = 45637
while num > 9:
total = 0
while num > 0:
digit = num % 10
num = num // 10
total = total + digit
num = total
print(num)
This outputs:
7
One way:
while len(str(ans))>1:
ans = sum(map(int, str(ans)))
Full code:
num = int(input("Enter a positive number : "))
ans = num
while len(str(ans))>1:
ans = sum(map(int, str(ans)))
print(ans)
Output for input 45637:
7

Find all the divisors of a number using Python [duplicate]

This question already has answers here:
What is the best way to get all the divisors of a number?
(18 answers)
Closed 2 years ago.
I am trying to write a Python script that will find prime numbers. In the way that I am going about this, the script is going to take a number from a range, determine the factors, determine if the only two factors are 1 and itself, and return true if it's a prime number. I need help with my code to find the factors.
num = int(input("enter: "))
i = 1
while i < num:
if num % i == 0:
print(i)
i += 1
else:
pass
The i is set to 1 because you can't divide by 0. However, the return starts to work, however, it only returns a couple of numbers, not the full list that I am hoping for. For example, you input 20, it returns only 1 and 2.
You're only incrementing i in the "is even" case. This'd fix it:
num = int(input("enter: "))
i = 1
while i < num:
if num % i == 0:
print(i)
i += 1
However, it's better to use range() for iterating over a range of numbers:
num = int(input("enter: "))
for i in range(1, num):
if num % i == 0:
print(i)

Why are complex arithmetic operation not allowed in the while condition in python [duplicate]

This question already has answers here:
Can we have assignment in a condition?
(10 answers)
Closed 4 years ago.
This function finds the number of digits in a number
def getNumOfDigits(i):
num = i
count = 1
num = num // 10
while num != 0:
count += 1
num //= 10
return count
If I try to modify the while condition to divide num by 10 and check if it is not equal to 0 it throws up a syntax error. Why is this so in python?
def getNumOfDigits(i):
num = i
count = 1
while (num //= 10)!= 0:
count += 1
return count
Assignment such as num //= 10 is a statement and cannot be part of an expression.
Why is this so in python?
Recently it was decided that assignment should be allowed in expressions in certain situations and this caused a huge controversy, see this article for more.

Categories