-2

The program requires a user to insert a number. Let's say we put 149. Now the program prints every number that has 0 digits in them till the number 149 (Including the number). So it's going to be 10,20,30,40,50,60,70,80,90,100,101...110..140 [Let's say the limit would be till 10000]

I have been trying to do this, but i only added +10 to every one, but that cannot be done >100 where it is 101,102..

  • 3
    Sounds like homework. So, what have you tried so far by yourself? – pzaenger Feb 25 '14 at 17:51
  • 1
    Have you tried converting the int to char array then check if the array contained `'0'`? – John Odom Feb 25 '14 at 17:51
  • I will give you a hint for another way to solve your problem: just work with the checksum for each digit. – pzaenger Feb 25 '14 at 17:56
  • Think recursion. Modulus operator might help. – devnull Feb 25 '14 at 17:58
  • The simplest approach would be to set up a `for` loop from `1` to `max` (where `max` is your `149`, for example) and call a function (that you would write) named `has_zero(n)` which returns `0` or `1` (false or true). Think about divide and remainders to figure out whether a number has any `0` digits in the number. Use `%` (modulo) and divide (`/`) in a loop. – lurker Feb 25 '14 at 17:58
  • Related: http://stackoverflow.com/questions/20945790/count-the-number-of-ks-between-0-and-n – herohuyongtao Feb 25 '14 at 18:07

4 Answers4

1

Use the function sprintf to convert an integer to a string and then search for the character '0' in the string. If found, then print the number. Here's a simple working program implementing this idea.

#include <stdio.h>
#include <string.h>

#define MAXLEN 50    // max number of digits in the input number

int main(void) {
    char buf[MAXLEN + 1];   // +1 for the null byte appended by sprintf
    char ch = '0';          // char to be searched for in buf
    int i, x;
    if(scanf("%d", &x) != 1) {   
        printf("Error in reading input.\n");
        return -1;
    }
    for(i = 1; i <= x; i++) {   
        sprintf(buf, "%d", i); // write i to the string buffer and append '\0'
        if(strchr(buf, ch))  // strchr returns a pointer to ch if found else NULL
            printf("%d\n", i);
    }
    return 0;
}

You can also extract each digit of an integer in the given range and check it for zero. Here's a naive implementation.

#include <stdio.h>

int main(void) {
    int i, x;
    int r;
    if(scanf("%d", &x) != 1) {
        printf("Error in reading input.\n");
        return -1;
    }
    for(i = 1; i <= x; i++) {
        for(r = i; r > 0; r /= 10) {
            if(r%10 == 0) {
                printf("%d\n", i);
                break;
            }
        }
    }
    return 0;
}
ajay
  • 9,402
  • 8
  • 44
  • 71
0

As a start, consider to convert each number to char [] and then check whether it contains a '0' or not.

To read on:

  1. How to check if a int var contains a specific number

  2. Count the number of Ks between 0 and N

Community
  • 1
  • 1
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
0

I think this would be the answer.

int j;
for(int i=1;i<150;i++){
   j=i;   
         while(j>0)
         {
         if(j%10==0)
         {
           printf("%d\n",i);
           break;
         }
         else 
           j=j/10;
        }
}
Arkku
  • 41,011
  • 10
  • 62
  • 84
Coldsteel48
  • 3,482
  • 4
  • 26
  • 43
0

The simple approach would be to iterate through all natural numbers up to the target number and testing each of them to see if they have any zero digits. Note that the last digit of a non-negative integer i can be obtained as the remainder from division by the base (i % 10 here). Also remember that integer division in C truncates decimals, e.g., (12 / 10) == 1

Arkku
  • 41,011
  • 10
  • 62
  • 84
  • It can be 101 which means it's 1 – user3130120 Feb 25 '14 at 18:34
  • @user3130120 If the number is `101` then your sequence should be: `(101%10 == 0) -> false`, `(101/10) -> 10`, `(10%0 == 0) -> true`. The remainder `% 10` only checks the last digit, like I said. So you need to keep dividing by `10` until you've checked all digits. (You can stop when the division gives zero, because leading zeroes don't count.) – Arkku Feb 25 '14 at 18:37