For a given
three-digit positive
integer, swap the first and last digits.
Input. One three-digit positive integer n (100 ≤ n
≤ 999).
Output. Print the number
obtained after the specified swap.
Sample
input |
Sample
output |
198 |
891 |
elementary
math
Algorithm analysis
Let
n = be
a three-digit number. Then:
·
The hundreds digit a is n / 100;
·
The tens digit b is n / 10 % 10;
·
The units digit c is n % 10;
As a result of swapping
the first and last digits, the number obtained will be:
c * 100 + b * 10
+ a
Algorithm implementation
Read
the three-digit number n.
scanf("%d",&n);
Compute the hundreds digit a, tens
digit b, and units digit c.
a = n / 100;
b = n / 10 % 10;
c = n % 10;
Compute and print the resulting number.
res = c * 100 +
b * 10 + a;
printf("%d\n",res);
Algorithm implementation – formatted input /
output
Read
the input data as the three digits a, b, and c of the
input number n.
scanf("%1d%1d%1d",&a,&b,&c);
Swap the digits. Print the result.
printf("%d%d%d\n",c,b,a);
Python implementation
Read
the three-digit number n.
n = int(input())
Compute the hundreds digit a, tens
digit b, and units digit c.
a = n // 100
b = n // 10 % 10
c = n % 10
Compute and print the resulting number.
res = c * 100 + b * 10 + a
print(res)
Python implementation
– digits
Read
the three-digit number n
as a string.
n = input()
Compute and print the resulting number.
res = 100 * int(n[2]) + 10 * int(n[1]) + int(n[0])
print(res)