# Sum and product of digits of a number in Python 3

Adding or multiplying digits of a number is a task that almost every programmer encounters. It allows you not only to check the ability to write code and think up correct algorithms, but also gives the opportunity to evaluate the syntax and functionality of the language used.

## The sum of numbers

The implementation of the program in different programming languages is not much different. But because of the concepts and peculiarities of syntax the solution in Python turns out to be shorter and simpler than, for example, the solution in C++. The full code of the program for finding the sum of digits of a number in Python looks like this

```num = int(input("Enter an integer: "))
sum = 0
while (num != 0):
sum = sum + num % 10
num = num // 10
print("The sum of the digits of the number is: ", sum)
```

The first line of the program asks for the number whose digits will be added. The second line is used to declare the variable in which the sum will be written. The variable must be equal to zero. Otherwise the obtained sum will be different from the real one (some people confuse addition and product of numbers, so they write one instead of zero). In the while loop the digits are added. On line 4, the program gets the lowest digit of the number. For example, if the number is 125, the program gets 5. The lowest digit is added to the sum variable. Line 5: The used low-order digit is discarded (divided by 10), that is, if it was 125, it becomes 12. It is necessary to use integer division, that is division without remainder, otherwise the digit will not be discarded, but will go to the fractional part of the result. On line 6, the program outputs the result of the summation to the console. Execution example:

```Enter an integer: 555
The sum of the digits of the number is: 15```

In this example, we used Python to calculate the sum of the digits of a three-digit number, 555.

## The product of the digits

The Python program for calculating the product of digits of a number has a similar structure and working principle. However, there are some important differences:

```num = int(input("Enter an integer: "))
mult = 1
while (num != 0):
mult = mult * (num % 10)
num = num // 10
print("The product of digits is equal to: ", mult)
```

First of all, the variable for storing the product is 1; if we assign it zero, the product itself will always be zero, too. Second, now in the while loop the next digit is not added to the variable, but is its multiplier. An important point is that because the multiplication operator has a higher execution priority than the remainder-of-division operator, the expression “num % 10” is placed in parentheses. If this is not done, the program will first multiply the variable `mult` by a number and only then take the remainder from it. An example of how the program works:

```Enter an integer: 55
The product of digits is equal to: 25
```

## Particular cases

The above variants of programs are the simplest ones. However, there are many more special cases.

### A number is given by a string

When a program receives input, the interpreter automatically assigns a string type to the variable and an error occurs if you try to use that variable in a calculation. In the scripts reviewed, this problem has been solved as follows:

```num = int(input("Enter an integer: "))
```
The input statement is placed in an int function that converts the resulting string to an integer. In some cases it may be more convenient not to convert the number immediately, but just before the calculation. An example of this case is shown below.

### A fractional number is entered

If the user enters a fractional number, the standard implementation stops working. The fractional part is not taken into account, since the program divides the number by 10. Even if you convert the entered number to an integer using `int`, all fractional digits will be discarded. Consider a program to calculate the product of digits of a fractional number (similar to the sum):

```num = input("Enter fractional: ")
# divide the input (data type string) into two parts
x = num.split(".")
a = int(x) # the integer part
b = int(x) # fractional part
mult = 1
while (a != 0): # multiply the integer part
mult = mult * (a % 10)
a = a // 10
while (b != 0): # multiply the numbers of the fractional part
mult = mult * (b % 10)
b = b // 10
print("The product of digits is equal to:", mult)
```

Example program execution:

```Enter fractional: 55.5
Product of digits equal to: 125
```

This program correctly calculates the product of digits of any number:

• Integer.
• Fractional > 1.
• Fractional < 1.

The implementation of the program was specifically done through strings, using the `split` function to divide the entered number into fractional and integer parts. The fractional part is handled the same way as the integer part.

#### When converting to a real

The approach described above is chosen because due to the inaccurate representation of decimal fractions in binary form (in which the computer works), the actual representation of the number differs from what is expected. As a result, if the user enters the number 0.55, if we convert it to a real number using `float`, the value 0.54999…8 is written into the variable. As a result, the result is inaccurate if you limit the number of iterations of the loop, or the loop is infinite, example:

```num = float(input("Enter fractional: ")) # Convert string to fractional
a = int(num) # the integer part, e.g., 5
b = num - int(num) # fractional part, for example 0.55
print("a =", a)
print("b =", b)
mult = 1
while (a != 0): # multiply whole numbers
mult = mult * (a % 10)
a = a // 10
while (b != 0): # b will never be 0
mult = mult * int(b*10) # 0.55 * 10 = 5.5, int(5.5) = 5
b = b * 10 - int(b * 10)
print("The product of the digits is:", mult)
```

If the real value of 5.55 were written into the variable, the program would have worked fine. However, the variable b actually holds 0.54999… This leads to an infinite allocation and discarding of the high digit. Here is an example of the output of the resulting program:

```Enter fractional: 5.55
a = 5
b = 0.5499999999999998
The product of the numbers is: 0
```

The program outputs the variable `b` as 0.54999999999999999998, after multiplying by 10, its value is already 5.49999999999999998. If you subtract 5 from that number, you get 0.499999999999999999999982. The number 2 is added at the end! So many iterations took place, and finally b became 0. During the multiplications, there were zeros among the digits of b. That is why the result is 0.