Specific digit count in an integer in C

arrays, c

Solution

So, you've already found that you can convert `1234` to `123` (that is, remove the least significant digit) by using `number / 10`.

If we wanted to acquire the least significant digit, we could use `number % 10`. For `1234`, that would have the value of `4`.

Understanding this, we can then modify your code to take this into account:

int main() {
    int n, count = 0;

    printf("Enter an integer number:");
    scanf("%d",&n);

    while (n != 0) {
        if (n % 10 == 1)
            count++;
        n /= 10;
    }

    printf("Number of 1s in your number: %d", count);
    return 0;
}

Problem

For example: if user input is 11234517 and wants to see the number of 1's in this input, output will be "number of 1's is 3. i hope you understand what i mean. i am only able to count number of digits in an integer. ``` #include <stdio.h> int main() { int n, count = 0; printf("Enter an integer number:"); scanf("%d",&n); while (n != 0) { n/=10; count++; } printf("Digits in your number: %d",count); return 0; } ``` maybe arrays are the solution. Any help would be appreciated. thank you!

Original source