0010 - Suma Cifrelor: Difference between revisions

From Bitnami MediaWiki
Pop Giulia (talk | contribs)
Pop Giulia (talk | contribs)
Line 1: Line 1:
Se da un numar natural. Calculaţi suma cifrelor numărului dat.
numar = int(input("Introduceti un numar natural: "))
suma_cifre = 0
while numar > 0:
    cifra = numar % 10
    suma_cifre += cifra
    numar //= 10
print("Suma cifrelor este:", suma_cifre)

Revision as of 11:57, 1 April 2023

numar = int(input("Introduceti un numar natural: ")) suma_cifre = 0 while numar > 0:

   cifra = numar % 10
   suma_cifre += cifra
   numar //= 10

print("Suma cifrelor este:", suma_cifre)