While loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

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.

10th Jan 2021, 4:50 PM
Python123
Python123 - avatar
12 Answers
+ 5
Python123 Are you asking for solution or giving assignment?
10th Jan 2021, 4:52 PM
A͢J
A͢J - avatar
+ 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
12th Jan 2021, 10:30 AM
A͢J
A͢J - avatar
+ 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
12th Jan 2021, 3:38 PM
Abhiyantā
Abhiyantā - avatar
+ 4
Python123 Show your attempts.
10th Jan 2021, 4:55 PM
A͢J
A͢J - avatar
+ 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)
10th Jan 2021, 5:00 PM
A͢J
A͢J - avatar
+ 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
10th Jan 2021, 5:02 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Sorry, solution..
10th Jan 2021, 4:53 PM
Python123
Python123 - avatar
+ 1
n = int(input()) length = 0 while n > 0: n //= 10 length += 1 print(length)
10th Jan 2021, 5:00 PM
Python123
Python123 - avatar
+ 1
We need to change this code to get that answer
10th Jan 2021, 5:00 PM
Python123
Python123 - avatar
+ 1
There is no output for your code AJ!
10th Jan 2021, 5:08 PM
Python123
Python123 - avatar
+ 1
Python123 The code provided by I Am AJ ! does provide an output
10th Jan 2021, 7:19 PM
Rik Wittkopp
Rik Wittkopp - avatar
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.
28th Jan 2021, 3:41 PM
We Doru
We Doru - avatar