answersLogoWhite

0

In python 3 I would write the following (assuming the digits are input by a user):

#!/usr/bin/python

a = input("Type a number: ")

sum = 0

for i in a: #the for next loop performs the function of a while loop with less lines of code.

#(My formating keeps getting deleted when I publish this answer. There should always be a tab- strictly speaking four spaces before this line) sum = sum + int(i)

sum = str(sum)

print("Your number was", a + ". The sum of its digits is" , sum + ".")

As you can see, the for next loop iterates through each value in the string "a", and sets its value to "i". For each iteration, the current value of i is added to the variable "sum" until the end of the number is reached. The program then prints the original number, and the sum of its digits.

User Avatar

Wiki User

15y ago

What else can I help you with?

Related Questions

Write c program to print palindrome using string without using strrev strlen strcam?

sdfdg


Without using a semicolom you can print any charectar in c language?

how can we print without using semi coloum semi coloum ina c program is used to indicate the end of a line......without ; at end the compiler cannot proceed to next step


You want to write a simple without using pointer or array c program which will print greatest number when you give 20 number?

i want to write a simple without using pointer or array c program which will print greatest number when i give 20 number .........How far have you gotten so far?


How do you print from compaq laptop?

Use the option File / Print in the program you are using.


Can you print string without using semi column in printf statement in c programming?

If you forget the semicolon, your program won't compile.


How do you print without using Print Preview?

impossible sorry


What is the uses of quick print option?

Quick Print enables you to print without using the Print dialog box. If you just want to do a standard print, without changing settings then using the Quick Print saves some time.


How can print pictures without using the printer?

You can't. A printer is necessary to print pictures.


What is the smallest number that can be formed using the digits 492130 and 5 without repeating any of the digits?

1023459


How can I frame a print without using glass?

To frame a print without using glass, you can consider using alternative materials such as acrylic, plexiglass, or UV-protective coatings. These options can provide protection for the print while avoiding the use of traditional glass.


Using while loop write a program which calculates the product of digits from 1 to 5 and also show these nos vertically?

Using while loop, write a program which calculates the product of digits from 1 to 5 and also show these no's vertically.


Is it possible to print matrix without using array?

Yes but why.