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 = 6The first digit can be isolated if you divide the given number by 1000 to get the integer quotient

 

$digit1 = intval(6753 / 1000)

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

 

$r = 6753 % 1000

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

 

$digit2 = intval(753 / 100)

Remaining digits = 53The remaining digits are now

 

$r = 753 % 100

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

 

$digit3 = intval(53 / 10)

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

 

$digit4 = 53 % 10

Java

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

 

digit1 = (int)(6753 / 1000)

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

 

r = 6753 % 1000

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

 

digit2 = (int)(753 / 100)

Remaining digits = 53The remaining digits are now

 

r = 753 % 100

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

 

digit3 = (int)(53 / 10)

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

 

digit4 = 53 % 10

C++

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

 

digit1 = (int)(6753 / 1000)

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

 

r = 6753 % 1000

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

 

digit2 = (int)(753 / 100)

Remaining digits = 53The remaining digits are now

 

r = 753 % 100

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

 

digit3 = (int)(53 / 10)

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

 

digit4 = 53 % 10

C#

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

 

digit1 = (int)(6753 / 1000)

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

 

r = 6753 % 1000

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

 

digit2 = (int)(753 / 100)

Remaining digits = 53The remaining digits are now

 

r = 753 % 100

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

 

digit3 = (int)(53 / 10)

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

 

digit4 = 53 % 10

Visual Basic

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

 

digit1 = 6753 \ 1000

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

 

r = 6753 Mod 1000

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

 

digit2 = 753 \ 100

Remaining digits = 53The remaining digits are now

 

r = 753 Mod 100

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

 

digit3 = 53 \ 10

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

 

digit4 = 53 Mod 10

Python

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

 

digit1 = 6753 // 1000

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

 

r = 6753 % 1000

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

 

digit2 = 753 // 100

Remaining digits = 53The remaining digits are now

 

r = 753 % 100

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

 

digit3 = 53 // 10

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

 

digit4 = 53 % 10

The program that solves this algorithm is shown here.

PHP

<?php
  echo "Enter a four-digit integer: ";
  $number = trim(fgets(STDIN));
   
  $digit1 = intval($number / 1000);
  $r = $number % 1000;
  
  $digit2 = intval($r / 100);
  $r = $r % 100;

  $digit3 = intval($r / 10);
  $digit4 = $r % 10;
  
  $sum = $digit1 + $digit2 + $digit3 + $digit4;
  echo $sum;
?>

Java

public static void main(String[] args) throws java.io.IOException {
  java.io.BufferedReader cin = new java.io.
          BufferedReader(new java.io.InputStreamReader(System.in));
  int digit1, digit2, digit3, digit4, number, r, sum;

  System.out.print("Enter a four-digit integer: ");
  number = Integer.parseInt(cin.readLine());
   
  digit1 = (int)(number / 1000);
  r = number % 1000;
  
  digit2 = (int)(r / 100);
  r = r % 100;

  digit3 = (int)(r / 10);
  digit4 = r % 10;
  
  sum = digit1 + digit2 + digit3 + digit4;
  System.out.println(sum);
}

C++

#include <iostream>
using namespace std;
int main() {
  int digit1, digit2, digit3, digit4, number, r, sum;

  cout << "Enter a four-digit integer: ";
  cin >> number;
   
  digit1 = (int)(number / 1000);
  r = number % 1000;
  
  digit2 = (int)(r / 100);
  r = r % 100;

  digit3 = (int)(r / 10);
  digit4 = r % 10;
  
  sum = digit1 + digit2 + digit3 + digit4;
  cout << sum;
  return 0;
}

C#

static void Main() {
  int digit1, digit2, digit3, digit4, number, r, sum;

  Console.Write("Enter a four-digit integer: ");
  number = Int32.Parse(Console.ReadLine());
   
  digit1 = (int)(number / 1000);
  r = number % 1000;
  
  digit2 = (int)(r / 100);
  r = r % 100;

  digit3 = (int)(r / 10);
  digit4 = r % 10;
  
  sum = digit1 + digit2 + digit3 + digit4;
  Console.Write(sum);
  Console.ReadKey();
}

Visual Basic

Sub Main()
  Dim digit1, digit2, digit3, digit4, number, r, sum As Integer

  Console.Write("Enter a four-digit integer: ")
  number = Console.ReadLine()

  digit1 = number \ 1000
  r = number Mod 1000

  digit2 = r \ 100
  r = r Mod 100

  digit3 = r \ 10
  digit4 = r Mod 10

  sum = digit1 + digit2 + digit3 + digit4
  Console.Write(sum)

  Console.ReadKey()
End Sub

Python

number = int(input("Enter a four-digit integer: "))

digit1 = number // 1000
r = number % 1000

digit2 = r // 100
r = r % 100

digit3 = r // 10
digit4 = r % 10

total = digit1 + digit2 + digit3 + digit4
print(total)

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

number = int(input("Enter a four-digit integer: "))

digit1, r = divmod(number, 1000)
digit2, r = divmod(r, 100)
digit3, digit4 = divmod(r, 10)

total = digit1 + digit2 + digit3 + digit4
print(total)

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 = 3The fourth digit can be isolated if you divide the given number by 10 to get the integer remainder

 

$digit1 = 6753 % 10

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

 

$r = intval(6753 / 10)

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

 

$digit3 = 675 % 10

Remaining digits = 67The remaining digits are now

 

$r = intval(675 / 10)

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

 

$digit3 = 67 % 10

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

 

$digit1 = intval(67 / 10)

Java

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

 

digit1 = 6753 % 10

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

 

r = (int)(6753 / 10)

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

 

digit3 = 675 % 10

Remaining digits = 67The remaining digits are now

 

r = (int)(675 / 10)

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

 

digit3 = 67 % 10

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

 

digit1 = (int)(67 / 10)

C++

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

 

digit1 = 6753 % 10

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

 

r = (int)(6753 / 10)

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

 

digit3 = 675 % 10

Remaining digits = 67The remaining digits are now

 

r = (int)(675 / 10)

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

 

digit3 = 67 % 10

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

 

digit1 = (int)(67 / 10)

C#

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

 

digit1 = 6753 % 10

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

 

r = (int)(6753 / 10)

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

 

digit3 = 675 % 10

Remaining digits = 67The remaining digits are now

 

r = (int)(675 / 10)

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

 

digit3 = 67 % 10

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

 

digit1 = (int)(67 / 10)

Visual Basic

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

 

digit1 = 6753 Mod 10

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

 

r = 6753 \ 10

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

 

digit3 = 675 Mod 10

Remaining digits = 67The remaining digits are now

 

r = 675 \ 10

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

 

digit3 = 67 Mod 10

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

 

digit1 = 67 \ 10

Python

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

 

digit1 = 6753 % 10

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

 

r = 6753 // 10

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

 

digit3 = 675 % 10

Remaining digits = 67The remaining digits are now

 

r = 675 // 10

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

 

digit3 = 67 % 10

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

 

digit1 = 67 // 10

The program for this algorithm is shown here.

PHP

<?php
  echo "Enter a four-digit integer: ";
  $number = trim(fgets(STDIN));
   
  $digit4 = $number % 10;
  $r = intval($number / 10);
  
  $digit3 = $r % 10;
  $r = intval($r / 10);

  $digit2 = $r % 10;
  $digit1 = intval($r / 10);
  
  $sum = $digit1 + $digit2 + $digit3 + $digit4;
  echo $sum;
?>

Java

public static void main(String[] args) throws java.io.IOException {
  java.io.BufferedReader cin = new java.io.
          BufferedReader(new java.io.InputStreamReader(System.in));
  int digit1, digit2, digit3, digit4, number, r, sum;

  System.out.print("Enter a four-digit integer: ");
  number = Integer.parseInt(cin.readLine());
   
  digit4 = number % 10;
  r = (int)(number / 10);
  
  digit3 = r % 10;
  r = (int)(r / 10);

  digit2 = r % 10;
  digit1 = (int)(r / 10);
  
  sum = digit1 + digit2 + digit3 + digit4;
  System.out.println(sum);
}

C++

#include <iostream>
using namespace std;
int main() {
  int digit1, digit2, digit3, digit4, number, r, sum;

  cout << "Enter a four-digit integer: ";
  cin >> number;
   
  digit4 = number % 10;
  r = (int)(number / 10);
  
  digit3 = r % 10;
  r = (int)(r / 10);

  digit2 = r % 10;
  digit1 = (int)(r / 10);
  
  sum = digit1 + digit2 + digit3 + digit4;
  cout << sum;
  return 0;
}

C#

static void Main() {
  int digit1, digit2, digit3, digit4, number, r, sum;

  Console.Write("Enter a four-digit integer: ");
  number = Int32.Parse(Console.ReadLine());
   
  digit4 = number % 10;
  r = (int)(number / 10);
  
  digit3 = r % 10;
  r = (int)(r / 10);

  digit2 = r % 10;
  digit1 = (int)(r / 10);
  
  sum = digit1 + digit2 + digit3 + digit4;
  Console.Write(sum);

  Console.ReadKey();
}

Visual Basic

Sub Main()
  Dim digit1, digit2, digit3, digit4, number, r, sum As Integer

  Console.Write("Enter a four-digit integer: ")
  number = Console.ReadLine()

  digit4 = number Mod 10
  r = number \ 10

  digit3 = r Mod 10
  r = r \ 10

  digit2 = r Mod 10
  digit1 = r \ 10

  sum = digit1 + digit2 + digit3 + digit4
  Console.Write(sum)

  Console.ReadKey()
End Sub

Python

number = int(input("Enter a four-digit integer: "))

digit4 = number % 10
r = number // 10

digit3 = r % 10
r = r // 10

digit2 = r % 10
digit1 = r // 10

total = digit1 + digit2 + digit3 + digit4
print(total)

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

number = int(input("Enter a four-digit integer: "))

r, digit4 = divmod(number, 10)
r, digit3 = divmod(r, 10)
digit1, digit2 = divmod(r, 10)

total = digit1 + digit2 + digit3 + digit4
print(total)