Write a program in C that will take a natural number n and output the smallest natural number greater or equal to n whose all digits are even in decimal. For example, if n = 16, it should output 20, and if n = 41, it should output 42. What I have tried:

#include <stdio.h>

int main(void) {
  int n, i, condition = 0;

  scanf("%d", &n);
  for (i = n; i >= n; i++) {
    if (i % 2 == 0) {
      i = i / 10;
      while (i > 0) {
        if (i % 2 == 0) {
          i = i / 10;
          condition = 1;
        } else {
          break;
        }
      }
    } else {
      condition = 0;
    }
    if (condition == 1) {
      printf("%d", i);
      break;
    }
  }
  return 0;
}

Why doesn't this work?

3

There are 3 answers

10
Sanjay-sopho On BEST ANSWER

You're printing the i after making it 0 i.e dividing it by 10 until first digit comes. So, it'll print 0 in every case.

Instead you should try assigning the value of i to another variable so you don't have to change i. Also after it becomes 0 by first iteration it won't make other iterations due to the requirement i>=n in the for loop.

#include <stdio.h>

int main(void) {
int n;
scanf("%d", &n);
for (int i = n; i >= n; i++){
    int num = i;
    int cond = 0;
    while(num>0){
        int dig = num%10;
        if(dig%2 != 0){
        cond = 1;
        break;
        }
    num/=10;
    }
    if (cond ==0){
        printf("%d",i);
        break;
    }
 }
 return 0;
}
0
RoadRunner On

Adding to @Sanjay-sopho answer, You can try two approaches:

  • Iterative, brute force.
  • recursion.

This is what the iterative version looks like:

#include <stdio.h>
#include <stdlib.h>

int check_even(int n);

int
main(void) {
    int i, n;

    printf("Enter n: ");
    if (scanf("%d", &n) != 1 || n < 1) {
        printf("Invalid entry, must be a natural number!\n");
        exit(EXIT_FAILURE);
    }

    for (i = n; i >= n; i++) {
        if (check_even(i)) {
            printf("%d\n", i);
            break;
        }
    }

    return 0;
}

int
check_even(int n) {
    int i;

    for (i = n; i > 0; i /= 10) {
        if (i % 2 != 0) {
            return 0;
        }
    }
    return 1;
}

Or you can try this recursive function:

int
check_even_rec(int n) {

    if (n == 0) {
        return 1;
    } else if (n % 2 != 0) {
        return 0;
    }

    return check_even_rec(n /= 10); 
}
0
AudioBubble On

The solutions published so far search all numbers greater than or equal to n until they find one with all-even digits. This is terribly inefficient.

A much better method is to scan the digits once from right to left and to adjust them to get the desired result directly:

// assume n >= 0                                                                
int next (int n)
{
    int r = 0; // result                                                        
    int c = 1; // coefficient (power of ten) of the current digit               

    // Scan digits from right to left.                                          
    while (n) {
        // current digit                                                        
        int d = n % 10;

        if (d % 2 == 0) {
            r += c * d;
        }
        else {
            // Make sure we handle the case d == 9 correctly.                   
            n++;
            d = n % 10;

            r += c * d;

            // Since we are incrementing the current digit (or a more           
            // significant one), we must set every less significant             
            // digits to zero.                                                   
            r -= r % c;
        }

        c *= 10;
        n /= 10;
    }

    return r;
}