DECIMAL NUMBERS
Decimal – Simple Definition A decimal number is a number written using the base-10 system. Decimal Numbers – Basic Idea To break a decimal number (example: 2345) into digits: no % 10 → gets the las...

Source: DEV Community
Decimal – Simple Definition A decimal number is a number written using the base-10 system. Decimal Numbers – Basic Idea To break a decimal number (example: 2345) into digits: no % 10 → gets the last digit no // 10 → removes the last digit This is the main logic used in all programs SUM OF DIGITS What does it do? It adds all the digits in a number Example: 2345 → 2 + 3 + 4 + 5 = 14 Loop Method We use a while loop to add digits one by one SUM OF DIGITS IN LOOP : no=2345 sum=0 while no>0: sum=sum+no%10 no=no//10 print(sum) OUTPUT : 14 Recursion Method The function calls itself again and again to repeat the process SUM OF DIGITS IN RECURSION : def sum1(no,sum): if no>0: sum=sum+no%10 no=no//10 return sum1(no,sum) return sum print(sum1(1234,0)) OUTPUT : 10 COUNT OF DIGIT What does it do? It counts how many digits are in a number Example: 2345 → 4 digits Loop Method In each step, we do count += 1 COUNT OF DIGITS IN LOOP : no=2345 count=0 while no>0: count+=1 no=no//10 print(count) O