2

I'm writing this program to find double digit odd numbers in a single number and I'm stuck.

For ex- In the number 56789 there are two double digit odd numbers: 67 & 89. I've written the code for this:

#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;
}

Here's my problem:

Input: 56789, output: "89\n78\n67\n56\n5\nOdd digit count is: 3"

Expected Output: 67 89 Total number of odd digit numbers: 2

Dock
  • 444
  • 5
  • 13
  • 1
    Side note: [always check the return value of `scanf`](https://stackoverflow.com/questions/10084224/how-do-we-test-the-return-values-from-the-scanf-function). – costaparas Feb 20 '21 at 14:01
  • 2
    Note that `conio.h` is not portable and completely unnecessary here. Why do you need it? I suggest removing that include. – costaparas Feb 20 '21 at 14:01
  • We were always taught that stdio.h and conio.h go hand-in-hand. Thanks I'll remember it from now on. – Advait Dandekar Feb 20 '21 at 14:02
  • 1
    Don't post text as images please – klutt Feb 20 '21 at 14:10
  • `conio.h` has never been a `C` standard include file; this will limit the computers on which your programme will compile. Unless you need it's system-dependant functionality, it's best if you don't include it. – Neil Feb 20 '21 at 23:18

1 Answers1

2

while(num>0) allows single and double digit numbers.
while(num>=10) allows only double digit numbers.

Werner Henze
  • 16,404
  • 12
  • 44
  • 69