C Exercise 89
100 Classic C Language Examples
**Problem:** A company transmits data over the public telephone network. The data is a four-digit integer and is encrypted during transmission. The encryption rules are as follows: add 5 to each digit, replace the digit with the remainder of the sum divided by 10, then swap the first and fourth digits, and swap the second and third digits.
**Analysis:** None.
Program source code:
Example
// Created by www.runoob.com on 15/11/9.
// Copyright (c) 2015 Runoob Tutorial. All rights reserved.
//
#include
<
stdio.h
int
main
(
)
{
int
a
,
i
,
aa
[
4
]
,
t;
printf
(
"
Please enter a four-digit number:
"
)
;
scanf
(
"
%d
"
,&
a
)
;
aa
[
0
]
=
a
%
10
;
aa
[
1
]
=
a
%
100
/
10
;
aa
[
2
]
=
a
%
1000
/
100
;
aa
[
3
]
=
a
/
1000
;
for
(
i
=
0
;
i
<=
3
;
i
++
)
{
aa
[
i
]
+=
5
;
aa
[
i
]
%=
10
;
}
for
(
i
=
0
;
i
<=
3
/
2
;
i
++
)
{
t
=
aa
[
i
]
;
aa
[
i
]
=
aa
[
3
i
]
;
aa
[
3
i
]
=
t
;
}
printf
(
"
The encrypted number:
"
)
;
for
(
i
=
3
;
i
=
0
;
i
--
)
printf
(
"
%d
"
,
aa
[
i
]
)
;
printf
(
"
\
n
"
)
;
}
The above example produces the following output:
请输入四位数字:1234
加密后的数字:9876