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
|
Remaining digits = 753 | The remaining digits can be isolated if you divide the given number by 1000 to get the integer remainder
|
Second digit = 7 | The second digit can be isolated if you divide the remaining digits by 100 to get the integer quotient
|
Remaining digits = 53 | The remaining digits are now
|
Third digit = 5 | The third digit can be isolated if you divide the remaining digits by 10 to get the integer quotient
|
Fourth digit = 3 | The last remaining digit, which happens to be the fourth digit, is
|
Java
First digit = 6 | The first digit can be isolated if you divide the given number by 1000 to get the integer quotient
|
Remaining digits = 753 | The remaining digits can be isolated if you divide the given number by 1000 to get the integer remainder
|
Second digit = 7 | The second digit can be isolated if you divide the remaining digits by 100 to get the integer quotient
|
Remaining digits = 53 | The remaining digits are now
|
Third digit = 5 | The third digit can be isolated if you divide the remaining digits by 10 to get the integer quotient
|
Fourth digit = 3 | The last remaining digit, which happens to be the fourth digit, is
|
C++
First digit = 6 | The first digit can be isolated if you divide the given number by 1000 to get the integer quotient
|
Remaining digits = 753 | The remaining digits can be isolated if you divide the given number by 1000 to get the integer remainder
|
Second digit = 7 | The second digit can be isolated if you divide the remaining digits by 100 to get the integer quotient
|
Remaining digits = 53 | The remaining digits are now
|
Third digit = 5 | The third digit can be isolated if you divide the remaining digits by 10 to get the integer quotient
|
Fourth digit = 3 | The last remaining digit, which happens to be the fourth digit, is
|
C#
First digit = 6 | The first digit can be isolated if you divide the given number by 1000 to get the integer quotient
|
Remaining digits = 753 | The remaining digits can be isolated if you divide the given number by 1000 to get the integer remainder
|
Second digit = 7 | The second digit can be isolated if you divide the remaining digits by 100 to get the integer quotient
|
Remaining digits = 53 | The remaining digits are now
|
Third digit = 5 | The third digit can be isolated if you divide the remaining digits by 10 to get the integer quotient
|
Fourth digit = 3 | The last remaining digit, which happens to be the fourth digit, is
|
Visual Basic
First digit = 6 | The first digit can be isolated if you divide the given number by 1000 to get the integer quotient
|
Remaining digits = 753 | The remaining digits can be isolated if you divide the given number by 1000 to get the integer remainder
|
Second digit = 7 | The second digit can be isolated if you divide the remaining digits by 100 to get the integer quotient
|
Remaining digits = 53 | The remaining digits are now
|
Third digit = 5 | The third digit can be isolated if you divide the remaining digits by 10 to get the integer quotient
|
Fourth digit = 3 | The last remaining digit, which happens to be the fourth digit, is
|
Python
First digit = 6 | The first digit can be isolated if you divide the given number by 1000 to get the integer quotient
|
Remaining digits = 753 | The remaining digits can be isolated if you divide the given number by 1000 to get the integer remainder
|
Second digit = 7 | The second digit can be isolated if you divide the remaining digits by 100 to get the integer quotient
|
Remaining digits = 53 | The remaining digits are now
|
Third digit = 5 | The third digit can be isolated if you divide the remaining digits by 10 to get the integer quotient
|
Fourth digit = 3 | The last remaining digit, which happens to be the fourth digit, is
|
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 = 3 | The fourth digit can be isolated if you divide the given number by 10 to get the integer remainder
|
Remaining digits = 675 | The remaining digits can be isolated if you divide the given number by 10 to get the integer quotient
|
Third digit = 5 | The third digit can be isolated if you divide the remaining digits by 10 to get the integer remainder
|
Remaining digits = 67 | The remaining digits are now
|
Second digit = 7 | The second digit can be isolated if you divide the remaining digits by 10 to get the integer remainder
|
First digit = 6 | The last remaining digit, which happens to be the first digit, is
|
Java
Fourth digit = 3 | The fourth digit can be isolated if you divide the given number by 10 to get the integer remainder
|
Remaining digits = 675 | The remaining digits can be isolated if you divide the given number by 10 to get the integer quotient
|
Third digit = 5 | The third digit can be isolated if you divide the remaining digits by 10 to get the integer remainder
|
Remaining digits = 67 | The remaining digits are now
|
Second digit = 7 | The second digit can be isolated if you divide the remaining digits by 10 to get the integer remainder
|
First digit = 6 | The last remaining digit, which happens to be the first digit, is
|
C++
Fourth digit = 3 | The fourth digit can be isolated if you divide the given number by 10 to get the integer remainder
|
Remaining digits = 675 | The remaining digits can be isolated if you divide the given number by 10 to get the integer quotient
|
Third digit = 5 | The third digit can be isolated if you divide the remaining digits by 10 to get the integer remainder
|
Remaining digits = 67 | The remaining digits are now
|
Second digit = 7 | The second digit can be isolated if you divide the remaining digits by 10 to get the integer remainder
|
First digit = 6 | The last remaining digit, which happens to be the first digit, is
|
C#
Fourth digit = 3 | The fourth digit can be isolated if you divide the given number by 10 to get the integer remainder
|
Remaining digits = 675 | The remaining digits can be isolated if you divide the given number by 10 to get the integer quotient
|
Third digit = 5 | The third digit can be isolated if you divide the remaining digits by 10 to get the integer remainder
|
Remaining digits = 67 | The remaining digits are now
|
Second digit = 7 | The second digit can be isolated if you divide the remaining digits by 10 to get the integer remainder
|
First digit = 6 | The last remaining digit, which happens to be the first digit, is
|
Visual Basic
Fourth digit = 3 | The fourth digit can be isolated if you divide the given number by 10 to get the integer remainder
|
Remaining digits = 675 | The remaining digits can be isolated if you divide the given number by 10 to get the integer quotient
|
Third digit = 5 | The third digit can be isolated if you divide the remaining digits by 10 to get the integer remainder
|
Remaining digits = 67 | The remaining digits are now
|
Second digit = 7 | The second digit can be isolated if you divide the remaining digits by 10 to get the integer remainder
|
First digit = 6 | The last remaining digit, which happens to be the first digit, is
|
Python
Fourth digit = 3 | The fourth digit can be isolated if you divide the given number by 10 to get the integer remainder
|
Remaining digits = 675 | The remaining digits can be isolated if you divide the given number by 10 to get the integer quotient
|
Third digit = 5 | The third digit can be isolated if you divide the remaining digits by 10 to get the integer remainder
|
Remaining digits = 67 | The remaining digits are now
|
Second digit = 7 | The second digit can be isolated if you divide the remaining digits by 10 to get the integer remainder
|
First digit = 6 | The last remaining digit, which happens to be the first digit, is
|
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)