THINK FIRST·CODE LATER

← All labs

Digits of a four-digit number

Problem

Write Digits: read a four-digit integer (1000–9999) and print its digits, their sum, the number reversed, its last two digits and whether it is divisible by 9 (a number is divisible by 9 exactly when the sum of its digits is):

Digits: 2 0 2 6
Sum of digits: 10
Reversed: 6202
Last two digits: 26
Divisible by 9: false

Use only / and % — no strings and no loops.

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l4-4-data-1.txt

2026
What your program must print for l4-4-data-1.txt
Digits: 2 0 2 6
Sum of digits: 10
Reversed: 6202
Last two digits: 26
Divisible by 9: false

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l4-4-data-1.txt

Windows PowerShell does not understand <:

Get-Content l4-4-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.