While loop | Sololearn: Learn to code for FREE!

+2

While loop

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.

1/10/2021 4:50:14 PM

Python123

12 Answers

New Answer

+5

Python123 Are you asking for solution or giving assignment?

+5

Python123 Are you joking with me? Run this code if it will not work I will leave programming. https://code.sololearn.com/cPzW7v09vyL8/?ref=app

+5

n = int(input()) s = 0 while n>0 : k = n % 10 # storing last digit in variable k s = s + k # s will add per digit n = n // 10 # now except last digit all digits are there in n print(s) https://code.sololearn.com/cVW03bm8hStJ/?ref=app

+4

Python123 Show your attempts.

+4

Python123 Well tried but it's not like that. Do like this num = int (input()) sum = 0 while (num > 0): sum = sum + num % 10 num = num//10 print (sum)

+2

1. Create a variable (num) & assign your number to it 2. Create another variable (result) and assign 0 to it 3. Take your number & turn it into a string. 4. Iterate through the string so each digit can now be accessed 5. Turn each digit back into an integer and add them to your result variable 6. print result

+1

Sorry, solution..

+1

n = int(input()) length = 0 while n > 0: n //= 10 length += 1 print(length)

+1

We need to change this code to get that answer

+1

There is no output for your code AJ!

+1

Python123 The code provided by I Am AJ ! does provide an output

0

Which code or line is responsible for deleting Even Numbers. def delete_start_evens(lst): While (len(lst) >0 and lst[0]%2==0): lst = lst[1:] return lst Print(delete_start_evens([4, 8, 10, 30, 11, 12, 15]) OUTPUT is 11, 12, 15. My point: Here the while Loop will run because the list given satisfy the condition stated with it, that is, list has length higher than Zero And the list start from 4 Which also satisfy the another condition which clearly shows lst[0] must be even. So, the while Loop will run. That is the duty of while loop, think. I get that this much.HOPE THAT IS CORRECT. But what condition or code delete the even numbers before an odd is reached. BUT, WHAT IS LST=LST[1:] DOING HERE. AND WHICH CODE IS RESPONSIBLE TO CHECK AND DELETE EVEN ITEMS FROM THE LIST.WHY DO OUTPUT SHOWS EMPTY WHEN I PUT LST[3:] INSTEAD OF [1:].NEED CLARIFICATION.