Write a program that prompts the user to enter a four-digit integer and then calculates the sum of its digits.

Solution

What you should keep in mind here is that an input statement assigns the given four-digit integer to one single variable, and not to four individual variables.

So, first the program must split the integer into its four digits and assign each digit to a separate variable. Then it can calculate the sum of these four variables and get the required result. There are two approaches available.

First Approach

Let’s try to understand the first approach using an arithmetic example. Take the number 6753, for example.

PHP

First digit = 6 The first digit can be isolated if you divide the given number by 1000 to get the integer quotient

$digit1 = intval(6753 / 1000)

Remaining digits = 753 The remaining digits can be isolated if you divide the given number by 1000 to get the integer remainder

$r = 6753 % 1000

Second digit = 7 The second digit can be isolated if you divide the remaining digits by 100 to get the integer quotient

$digit2 = intval(753 / 100)

Remaining digits = 53 The remaining digits are now

$r = 753 % 100

Third digit = 5 The third digit can be isolated if you divide the remaining digits by 10 to get the integer quotient

$digit3 = intval(53 / 10)

Fourth digit = 3 The last remaining digit, which happens to be the fourth digit, is

$digit4 = 53 % 10

Java

First digit = 6 The first digit can be isolated if you divide the given number by 1000 to get the integer quotient

digit1 = (int)(6753 / 1000)

Remaining digits = 753 The remaining digits can be isolated if you divide the given number by 1000 to get the integer remainder

r = 6753 % 1000

Second digit = 7 The second digit can be isolated if you divide the remaining digits by 100 to get the integer quotient

digit2 = (int)(753 / 100)

Remaining digits = 53 The remaining digits are now

r = 753 % 100

Third digit = 5 The third digit can be isolated if you divide the remaining digits by 10 to get the integer quotient

digit3 = (int)(53 / 10)

Fourth digit = 3 The last remaining digit, which happens to be the fourth digit, is

digit4 = 53 % 10

C++

First digit = 6 The first digit can be isolated if you divide the given number by 1000 to get the integer quotient

digit1 = (int)(6753 / 1000)

Remaining digits = 753 The remaining digits can be isolated if you divide the given number by 1000 to get the integer remainder

r = 6753 % 1000

Second digit = 7 The second digit can be isolated if you divide the remaining digits by 100 to get the integer quotient

digit2 = (int)(753 / 100)

Remaining digits = 53 The remaining digits are now

r = 753 % 100

Third digit = 5 The third digit can be isolated if you divide the remaining digits by 10 to get the integer quotient

digit3 = (int)(53 / 10)

Fourth digit = 3 The last remaining digit, which happens to be the fourth digit, is

digit4 = 53 % 10

C#

First digit = 6 The first digit can be isolated if you divide the given number by 1000 to get the integer quotient

digit1 = (int)(6753 / 1000)

Remaining digits = 753 The remaining digits can be isolated if you divide the given number by 1000 to get the integer remainder

r = 6753 % 1000

Second digit = 7 The second digit can be isolated if you divide the remaining digits by 100 to get the integer quotient

digit2 = (int)(753 / 100)

Remaining digits = 53 The remaining digits are now

r = 753 % 100

Third digit = 5 The third digit can be isolated if you divide the remaining digits by 10 to get the integer quotient

digit3 = (int)(53 / 10)

Fourth digit = 3 The last remaining digit, which happens to be the fourth digit, is

digit4 = 53 % 10

Visual Basic

First digit = 6 The first digit can be isolated if you divide the given number by 1000 to get the integer quotient

digit1 = 6753 \ 1000

Remaining digits = 753 The remaining digits can be isolated if you divide the given number by 1000 to get the integer remainder

r = 6753 Mod 1000

Second digit = 7 The second digit can be isolated if you divide the remaining digits by 100 to get the integer quotient

digit2 = 753 \ 100

Remaining digits = 53 The remaining digits are now

r = 753 Mod 100

Third digit = 5 The third digit can be isolated if you divide the remaining digits by 10 to get the integer quotient

digit3 = 53 \ 10

Fourth digit = 3 The last remaining digit, which happens to be the fourth digit, is

digit4 = 53 Mod 10

Python

First digit = 6 The first digit can be isolated if you divide the given number by 1000 to get the integer quotient

digit1 = 6753 // 1000

Remaining digits = 753 The remaining digits can be isolated if you divide the given number by 1000 to get the integer remainder

r = 6753 % 1000

Second digit = 7 The second digit can be isolated if you divide the remaining digits by 100 to get the integer quotient

digit2 = 753 // 100

Remaining digits = 53 The remaining digits are now

r = 753 % 100

Third digit = 5 The third digit can be isolated if you divide the remaining digits by 10 to get the integer quotient

digit3 = 53 // 10

Fourth digit = 3 The last remaining digit, which happens to be the fourth digit, is

digit4 = 53 % 10


The program that solves this algorithm is shown here.

PHP

Java

C++

C#

Visual Basic

Python

This approach, however, can be refined a little using the divmod() function.

Second Approach

Once more, let’s try to understand the second approach using an arithmetic example. Take the same number, 6753, for example.

PHP

Fourth digit = 3 The fourth digit can be isolated if you divide the given number by 10 to get the integer remainder

$digit1 = 6753 % 10

Remaining digits = 675 The remaining digits can be isolated if you divide the given number by 10 to get the integer quotient

$r = intval(6753 / 10)

Third digit = 5 The third digit can be isolated if you divide the remaining digits by 10 to get the integer remainder

$digit3 = 675 % 10

Remaining digits = 67 The remaining digits are now

$r = intval(675 / 10)

Second digit = 7 The second digit can be isolated if you divide the remaining digits by 10 to get the integer remainder

$digit3 = 67 % 10

First digit = 6 The last remaining digit, which happens to be the first digit, is

$digit1 = intval(67 / 10)

Java

Fourth digit = 3 The fourth digit can be isolated if you divide the given number by 10 to get the integer remainder

digit1 = 6753 % 10

Remaining digits = 675 The remaining digits can be isolated if you divide the given number by 10 to get the integer quotient

r = (int)(6753 / 10)

Third digit = 5 The third digit can be isolated if you divide the remaining digits by 10 to get the integer remainder

digit3 = 675 % 10

Remaining digits = 67 The remaining digits are now

r = (int)(675 / 10)

Second digit = 7 The second digit can be isolated if you divide the remaining digits by 10 to get the integer remainder

digit3 = 67 % 10

First digit = 6 The last remaining digit, which happens to be the first digit, is

digit1 = (int)(67 / 10)

C++

Fourth digit = 3 The fourth digit can be isolated if you divide the given number by 10 to get the integer remainder

digit1 = 6753 % 10

Remaining digits = 675 The remaining digits can be isolated if you divide the given number by 10 to get the integer quotient

r = (int)(6753 / 10)

Third digit = 5 The third digit can be isolated if you divide the remaining digits by 10 to get the integer remainder

digit3 = 675 % 10

Remaining digits = 67 The remaining digits are now

r = (int)(675 / 10)

Second digit = 7 The second digit can be isolated if you divide the remaining digits by 10 to get the integer remainder

digit3 = 67 % 10

First digit = 6 The last remaining digit, which happens to be the first digit, is

digit1 = (int)(67 / 10)

C#

Fourth digit = 3 The fourth digit can be isolated if you divide the given number by 10 to get the integer remainder

digit1 = 6753 % 10

Remaining digits = 675 The remaining digits can be isolated if you divide the given number by 10 to get the integer quotient

r = (int)(6753 / 10)

Third digit = 5 The third digit can be isolated if you divide the remaining digits by 10 to get the integer remainder

digit3 = 675 % 10

Remaining digits = 67 The remaining digits are now

r = (int)(675 / 10)

Second digit = 7 The second digit can be isolated if you divide the remaining digits by 10 to get the integer remainder

digit3 = 67 % 10

First digit = 6 The last remaining digit, which happens to be the first digit, is

digit1 = (int)(67 / 10)

Visual Basic

Fourth digit = 3 The fourth digit can be isolated if you divide the given number by 10 to get the integer remainder

digit1 = 6753 Mod 10

Remaining digits = 675 The remaining digits can be isolated if you divide the given number by 10 to get the integer quotient

r = 6753 \ 10

Third digit = 5 The third digit can be isolated if you divide the remaining digits by 10 to get the integer remainder

digit3 = 675 Mod 10

Remaining digits = 67 The remaining digits are now

r = 675 \ 10

Second digit = 7 The second digit can be isolated if you divide the remaining digits by 10 to get the integer remainder

digit3 = 67 Mod 10

First digit = 6 The last remaining digit, which happens to be the first digit, is

digit1 = 67 \ 10

Python

Fourth digit = 3 The fourth digit can be isolated if you divide the given number by 10 to get the integer remainder

digit1 = 6753 % 10

Remaining digits = 675 The remaining digits can be isolated if you divide the given number by 10 to get the integer quotient

r = 6753 // 10

Third digit = 5 The third digit can be isolated if you divide the remaining digits by 10 to get the integer remainder

digit3 = 675 % 10

Remaining digits = 67 The remaining digits are now

r = 675 // 10

Second digit = 7 The second digit can be isolated if you divide the remaining digits by 10 to get the integer remainder

digit3 = 67 % 10

First digit = 6 The last remaining digit, which happens to be the first digit, is

digit1 = 67 // 10


The program for this algorithm is shown here.

PHP

Java

C++

C#

Visual Basic

Python

As with the previous example, this approach can be refined a little, using the divmod() function.

Leave a Reply

Your email address will not be published. Required fields are marked *

This article is taken from a book of mine. If you enjoyed it, please visit my author's page inand check out all of my books!