0

How do I solve this code coach problem?

How do I solve this while loop related problems kf code coach. This is the problem : The given program calculates and outputs the number of digits in a given number using a while loop. n = int(input()) length = 0 while n > 0: n //= 10 length += 1 print(length) During each iteration, the loop uses floor division to divide the given number by 10, thus dropping one digit. The process continues until the number has no more digits (n>0). You need to change the code to calculate and output the sum of all digits of the input number. Sample Input 643 Sample Output 13 Explanation The sum of the digits of 643 is 6+4+3 = 13. Hint: You can get each digit of the number by modulo dividing (%) it by 10 during each iteration. I tried this but this is not working : n = int(input()) length = 0 while n>0: x = n%10 length += n print (length)

26th Oct 2020, 5:32 PM
Aditya
Aditya - avatar
8 Respuestas
+ 12
You are giving n last digit to x then adding whole n to length Which wouldn't work. Also you are not changing n value so it will loop forever. Right Code: n = int(input()) sum = 0 while n > 0: sum += n%10 #Add last number n //= 10 #remove last number print(sum) Hope It Helps You 😊
26th Oct 2020, 6:29 PM
Hacker Badshah
Hacker Badshah - avatar
+ 2
fun main(args: Array<String>) { var num = readLine()!!.toInt() var x = 0 while (num>0) { x=x+num%10 num=num/10 } println(x) }
14th Sep 2022, 6:31 PM
Monika Raut
Monika Raut - avatar
+ 1
I didn't use the while loop for this code and the code worked with the same but I made a different script. fun main(args: Array<String>) { var num = readLine()!!.toInt() var hundred = (num - (num % 100)) / 100 var one = num % 10 var ten = (num % 100 - num % 10) / 10 var sum_parts = hundred + one + ten println(sum_parts) }
24th Dec 2022, 7:28 PM
Arman Jabari
Arman Jabari - avatar
0
#the best way: # while Loops(sum of digit) n = int(input()) digits = [int(d) for d in str(n)] print(sum(digits))
18th Oct 2021, 5:25 PM
Kyrylo
Kyrylo - avatar
0
n = int(input()) sum = 0 while n > 0: n, reminder = divmod(n, 10) sum += reminder print(sum)
21st Nov 2021, 9:35 AM
dasyn7
- 3
Let’s save some time by creating a program to do the calculation for you! Take a number N as input and output the sum of all numbers from 1 to N (including N). Sample Input 100 Sample Output 5050 Explanation: The sum of all numbers from 1 to 100 is equal to 5050.
14th Mar 2021, 9:09 AM
‎سيف الدين حسين‎
‎سيف الدين حسين‎ - avatar
- 4
Sum of Consecutive Numbers No one likes homework, but your math teacher has given you an assignment to find the sum of the first N numbers. Let’s save some time by creating a program to do the calculation for you! Take a number N as input and output the sum of all numbers from 1 to N (including N). Sample Input 100 Sample Output 5050
13th Mar 2021, 4:15 PM
Syed Fahad Ahmed
Syed Fahad Ahmed - avatar
- 7
change value for n: From 643 you get 3 by n%10 but next you need 4. (hint from 64) But your code always get only 3 Hint: Solution is already in the question's 1st part...
26th Oct 2020, 5:43 PM
Jayakrishna 🇮🇳