First Exercise
This commit is contained in:
13
Informatik_I/Examples/Week_1/README.md
Normal file
13
Informatik_I/Examples/Week_1/README.md
Normal file
@@ -0,0 +1,13 @@
|
||||
# Task
|
||||
|
||||
Write a program which reads in an integer a larger than `1000` and outputs its last three digits. For example, if `a=14325`, the output should be `3 2 5`.
|
||||
|
||||
_Hint_: You need to use integer division and modulo operators.
|
||||
|
||||
# Input
|
||||
|
||||
An integer larger than `1000`.
|
||||
|
||||
# Output
|
||||
|
||||
Last three digits separated by spaces.
|
22
Informatik_I/Examples/Week_1/main.cpp
Normal file
22
Informatik_I/Examples/Week_1/main.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
|
||||
int a;
|
||||
std::cin >> a; // get input of for a
|
||||
|
||||
if (a >= 1000) { // check if input is greater than 1000
|
||||
int b = a % 10; // get last digit
|
||||
a /= 10;
|
||||
int c = a % 10; // get second digit
|
||||
a /= 10;
|
||||
int d = a % 10; // get first digit
|
||||
|
||||
std::cout << d << " " << c << " "
|
||||
<< b; // output first, second, and last digit
|
||||
} else { // if input not greater than 1000 return 0
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user