gpt4 book ai didi

c - 我如何在循环的最后一项打印出不同的东西,C编程

转载 作者:太空宇宙 更新时间:2023-11-04 07:13:09 24 4
gpt4 key购买 nike

Please enter an integer: 1234
Digit: 4
Digit: 3
Digit: 2
Digit: 1

我现在有一个程序可以反向打印整数的数字。而不是上面的输出,我想要:

Please enter an integer: 1234
Digit: 4
Digit: 3
Digit: 2
Last Digit: 1

我该如何做到这一点?顺便说一句,这是我的代码:

#include <stdio.h>
int main()
{
int input;
printf("Please enter an integer: ");
scanf("%d", &input);
int num = input;
int i = 0;
while(num > 0)
{
int remainder = num % 10;
num = num /10;
i++;
printf("Digit: %d\n", remainder );
}
return 0;
}

如何在循环的最后一项打印其他内容?

最佳答案

使用

if (num > 0) //Indicates that there are more digits to come
{
printf("Digit: %d\n", remainder );
}
else // Otherwise, it is the last digit
{
printf("Last Digit: %d\n", remainder);
}

不仅仅是

printf("Last Digit: %d\n", remainder);

关于c - 我如何在循环的最后一项打印出不同的东西,C编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26793210/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com