Answer to Question #314441 in C for Mona

Question #314441

For a given number N(0<N<100), little johnny wants to find out the minimum positive integer X divisible by N,where the sum of digits of X is equal to N,and X is not equal to N

1
Expert's answer
2022-03-19T12:25:05-0400
#include <stdio.h>

long sum_digits(long n) {
    long sum = 0;

    while (n > 0) {
        sum += n%10;
        n /= 10;
    }
    return sum;
}


int main() {
    long n, x;

    scanf("%ld", &n);
    x = 2*n;
    while (1) {
        if (sum_digits(x) == n) {
            break;
        }
        x += n;
    }
    printf("%ld", x);

    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS