【问题标题】:I'm writing this program to find double digit odd numbers in a single number and I'm stuck我正在编写此程序以在单个数字中查找两位数的奇数,但我被卡住了
【发布时间】:2021-05-23 07:36:02
【问题描述】:

我正在编写这个程序来查找单个数字中的两位数奇数,但我被卡住了。

例如- 在数字 56789 中有两个两位数的奇数:67 和 89。我为此编写了代码:

#include<stdio.h>
#include<conio.h>
int main()
{
    int num,rem=0;
    int odd=0;
    printf("Enter a number: ");
    scanf("%d",&num);
    while(num>0)
    {
        rem=(num%100);
        printf("%d\n",rem);
        if(rem%2!=0)
        {
            odd++;
        }
        num=num/10;
        
    }
    printf("Odd digits count is: %d",odd);
    return 0;
}

这是我的问题:

预期输出: 67 89 奇数总数:2个

【问题讨论】:

  • 请注意,conio.h 不可移植,在这里完全没有必要。你为什么需要它?我建议删除该包含。
  • 我们总是被告知 stdio.h 和 conio.h 是齐头并进的。谢谢,从现在开始我会记住的。
  • 请不要以图片形式发布文字
  • conio.h 从来都不是C 标准包含文件;这将限制您的程序将在其上编译的计算机。除非您需要它的系统相关功能,否则最好不要包含它。

标签: c


【解决方案1】:

while(num&gt;0) 允许使用一位数和两位数。
while(num&gt;=10) 仅允许使用两位数。

【讨论】:

    猜你喜欢
    • 2021-12-31
    • 2013-12-24
    • 1970-01-01
    • 2021-07-24
    • 2023-02-09
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    相关资源
    最近更新 更多