c语言代码,输入一个四位数,将其加密后输出。方法是将该数每一位上的数字加9,然后除以10取余,做为该位上的新数字,最后将千位和十位上的数字互换,百位和个位上的数字互换,组成加密后的新四位数。例如输入1
时间: 2024-06-16 13:05:24 浏览: 21
C语言代码如下:
```c
#include <stdio.h>
int main() {
int num, encryptedNum, temp;
printf("请输入一个四位数:");
scanf("%d", &num);
// 加密操作
num = (num + 9999) % 10000; // 防止输入的数大于9999
// 每一位上的数字加9并取余
num = ((num / 1000 + 9) % 10) * 1000 + ((num / 100 % 10 + 9) % 10) * 100 + ((num / 10 % 10 + 9) % 10) * 10 + ((num % 10 + 9) % 10);
// 交换千位和十位上的数字
temp = num / 1000;
num = num - temp * 1000;
num = num + temp * 10;
// 交换百位和个位上的数字
temp = num / 100;
num = num - temp * 100;
num = num + temp * 1000;
printf("加密后的数字为:%d\n", num);
return 0;
}
```
相关问题
完成下面程序: 输入1个四位数,将其加密后输出。 方法是将该数每一位上的数字加9,然后除以10取余,做为该位上的新数字, 最后将第1位和第3位上的数字互换,第2位和第4位上的数字互换,组成加密后的新数。用c++
### 回答1:
以下是代码实现:
```cpp
#include <iostream>
using namespace std;
int main() {
int num, digit1, digit2, digit3, digit4;
cout << "请输入一个四位数:";
cin >> num;
// 拆分每一位数字
digit1 = num / 1000;
digit2 = num % 1000 / 100;
digit3 = num % 100 / 10;
digit4 = num % 10;
// 每一位数字加9并除以10取余
digit1 = (digit1 + 9) % 10;
digit2 = (digit2 + 9) % 10;
digit3 = (digit3 + 9) % 10;
digit4 = (digit4 + 9) % 10;
// 交换数字
int temp = digit1;
digit1 = digit3;
digit3 = temp;
temp = digit2;
digit2 = digit4;
digit4 = temp;
// 输出加密后的数字
cout << "加密后的数字为:" << digit1 << digit2 << digit3 << digit4 << endl;
return 0;
}
```
运行结果示例:
```
请输入一个四位数:1234
加密后的数字为:8901
```
### 回答2:
以下为使用C语言完成该程序的示例代码:
```c
#include <stdio.h>
int main() {
int num, encryptedNum, temp;
printf("请输入一个四位数:");
scanf("%d", &num);
// 对每一位数字加9并取余得到新数字
int digit1 = (num / 1000 + 9) % 10;
int digit2 = (num / 100 % 10 + 9) % 10;
int digit3 = (num / 10 % 10 + 9) % 10;
int digit4 = (num % 10 + 9) % 10;
// 交换第1位和第3位上的数字
temp = digit1;
digit1 = digit3;
digit3 = temp;
// 交换第2位和第4位上的数字
temp = digit2;
digit2 = digit4;
digit4 = temp;
// 组成加密后的新数
encryptedNum = digit1 * 1000 + digit2 * 100 + digit3 * 10 + digit4;
printf("加密后的数字为:%d", encryptedNum);
return 0;
}
```
此程序实现了将一个四位数进行加密输出的功能。首先从用户输入中获取四位数,然后按照题目要求对每一位数字进行加9并取余得到新数字。接下来,根据题目要求交换第1位和第3位上的数字,交换第2位和第4位上的数字。最后,将四个新数字组合起来形成加密后的新数,并输出结果。
### 回答3:
```c
#include<stdio.h>
int main() {
int num, temp, digit1, digit2, digit3, digit4;
printf("请输入一个四位数:");
scanf("%d", &num);
// 拆分每一位上的数字
digit1 = num / 1000;
digit2 = (num % 1000) / 100;
digit3 = (num % 100) / 10;
digit4 = num % 10;
// 加密
digit1 = (digit1 + 9) % 10;
digit2 = (digit2 + 9) % 10;
digit3 = (digit3 + 9) % 10;
digit4 = (digit4 + 9) % 10;
// 互换数字
temp = digit1;
digit1 = digit3;
digit3 = temp;
temp = digit2;
digit2 = digit4;
digit4 = temp;
// 输出加密后的数字
printf("加密后的数字:%d%d%d%d\n", digit1, digit2, digit3, digit4);
return 0;
}
```
这段程序通过输入一个四位数,对每一位上的数字进行加密,并按照要求进行互换,最后输出加密后的数字。在加密过程中,每一位上的数字先加9,然后除以10取余,得到新的数字。接着,将第1位上的数字和第3位上的数字互换,第2位上的数字和第4位上的数字互换,组成加密后的新数。
编写c语言程序对数据进行加密 从键盘输入一个数对每一位数字均加2+2后大于9 则取其除十的余数
```
#include <stdio.h>
int main() {
int num;
printf("请输入要加密的数字:");
scanf("%d", &num);
int encrypted = 0;
int base = 1;
while (num > 0) {
int digit = num % 10;
digit = (digit + 2) % 10;
encrypted += digit * base;
num /= 10;
base *= 10;
}
printf("加密后的数字为:%d\n", encrypted);
return 0;
}
```
说明:
1. 首先从键盘输入一个数 `num`。
2. 接着定义一个变量 `encrypted` 用于存储加密后的数字,初始值为0。
3. 定义一个变量 `base` 用于记录加密后的数字的位数,初始值为1。
4. 使用 `while` 循环对 `num` 的每一位进行加密。
5. 每次循环,先取出 `num` 的个位数字,存入变量 `digit` 中。
6. 对 `digit` 进行加2操作,如果加2后大于9,则取其除十的余数。
7. 将加密后的数字累加到 `encrypted` 中。
8. 将 `num` 除以10,即将其个位数字去掉。
9. 将 `base` 乘以10,即将加密后的数字的位数加1。
10. 循环结束后,输出加密后的数字 `encrypted`。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)