c语言用while循环写出12393的数字顺序相反的数
时间: 2023-06-10 15:09:13 浏览: 40
可以使用取模运算和除法来实现:
```c
#include <stdio.h>
int main() {
int num = 12393;
int reverse = 0;
while (num > 0) {
int digit = num % 10; // 取出最低位的数字
reverse = reverse * 10 + digit; // 将数字加到反转后的数中
num /= 10; // 将最低位的数字去掉
}
printf("%d的数字顺序相反的数是%d\n", 12393, reverse);
return 0;
}
```
输出结果为:
```
12393的数字顺序相反的数是39321
```
相关问题
C语言写出随机生成100以内的不重复随机数并存入顺序表
可以使用C语言的rand()函数来生成随机数,并使用一个数组来存储已经生成的随机数,确保不会有重复的数字出现。下面是一个简单的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_SIZE 100
int main() {
int nums[MAX_SIZE];
int count = 0;
srand(time(NULL)); // 设置随机数种子
while (count < MAX_SIZE) {
int num = rand() % 100 + 1; // 生成1-100之间的随机数
int i;
for (i = 0; i < count; i++) {
if (nums[i] == num) { // 如果已经存在相同的数字,则重新生成
break;
}
}
if (i == count) { // 如果没有重复的数字,则存入数组中
nums[count] = num;
count++;
}
}
// 输出生成的数字
int i;
for (i = 0; i < MAX_SIZE; i++) {
printf("%d ", nums[i]);
}
printf("\n");
return 0;
}
```
这段代码首先定义了一个大小为100的数组nums来存储随机数,然后通过srand()函数设置随机数种子。接着使用一个while循环来不断生成随机数,如果发现已经有相同的数字,则重新生成。如果没有重复的数字,则将其存入数组中,直到数组中存满100个数字。最后,使用一个for循环输出生成的随机数。
用c语言写一个10进制转16进制数的代码
以下是一个简单的 C 代码,用于将 10 进制数转换为 16 进制数:
```c
#include <stdio.h>
int main() {
int decimalNum, remainder, quotient;
int i = 1, j, temp;
char hexadecimalNum[100];
printf("Enter a decimal number: ");
scanf("%d", &decimalNum);
quotient = decimalNum;
while (quotient != 0) {
temp = quotient % 16;
if (temp < 10) {
temp += 48;
}
else {
temp += 55;
}
hexadecimalNum[i++] = temp;
quotient /= 16;
}
printf("Hexadecimal number: ");
for (j = i - 1; j > 0; j--) {
printf("%c", hexadecimalNum[j]);
}
return 0;
}
```
在此代码中,我们使用 `while` 循环来将 10 进制数转换为 16 进制数,并使用 `if...else` 语句将数字转换为字符。最后,我们使用 `for` 循环按相反的顺序打印出 16 进制数。