lbarros
lbarros

Reputation: 19

How to iterate over numbers in C

I've gotta count how many times a certain digit is repeated in each number in a range. For example, in the numbers between 0 and 20, there is only one occurrence of 1 being repeated twice (11). I originally did this by converting the int to a str and iterating over it, but I would like to be able to solve this in an arithmetic way. Any ideas?

Upvotes: 0

Views: 837

Answers (4)

Fe2O3
Fe2O3

Reputation: 8344

Riffing on the answer provided by @Abdo Salm presented only to demonstrate a slightly alternative approach. All credit to Abdo.

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

void process( int n, int cnts[] ) {
    // count occurrences of each digit (right to left)
    while( n )
        cnts[ abs(n % 10) ]++, n /= 10;
}

void report( int cnts[], int thresh ) {
    char *sep = "";
    for( int j = 0; j < 10; j++ )
        if( cnts[ j ] > thresh )
            printf( "%s%dx%d", sep, cnts[ j ], j ), sep = ", ";

    if( !*sep )
        printf( "no digit occurred multiple times" );

    putchar( '\n' );
}

int main( void ) {
    int ranges[][2] = {
        {   0,  10, },
        {   0,  22, },
        { 110, 133, },
        { 447, 448, },
    };

    // four trail ranges above
    for( int r = 0; r < sizeof ranges/sizeof ranges[0]; r++ ) {
        int metacnts[ 10 ] = {0};

        // examine each number in the range (inclusive of start & end)
        for( int i = ranges[r][0]; i <= ranges[r][1]; i++ ) {
            int cnts[ 10 ] = {0};

            process( i, cnts );

            // tabulate only 'digits' occurring more than once
            for( int j = 0; j < 10; j++ )
                if( cnts[ j ] > 1 )
                    metacnts[ j ]++;
        }

        // report only digits appearing multiple times in numbers between min & max
        printf( "Range %3d-%3d (incl) ", ranges[r][0], ranges[r][1] );
        report( metacnts, 0 );
    }

    return 0;
}

Output:

Range   0- 10 (incl) no digit occurred multiple times
Range   0- 22 (incl) 1x1, 1x2
Range 110-133 (incl) 12x1, 1x2, 1x3
Range 447-448 (incl) 2x4

Upvotes: 0

chux
chux

Reputation: 153338

how many times a certain digit is repeated in each number in a range.

Pseudo code*1

Get the range: imin, imax (any int pair where imin <= imax)

Get the digit: digit 0 to 9

Iterate m from imin to imax, inclusive

.... Print m

.... Set digit_count = 0

.... Repeat

....... Last digit ld of m is abs(m%10).

....... If ld == digit, increment digit_count.

........ Divide m by 10

.... Until m == 0

.... Print digit_count

Done


*1 As OP did not provide code, seemed best to not provide a code answer.

Upvotes: 0

abdo Salm
abdo Salm

Reputation: 1841

here is a general solution that you can use , your problem didn't contain much information so I assumed that you want to count the number of repeating of each digit in each number.

so what I did is like hashing where the digits in each number will never cross the value 9 , so they are from 0 to 9 so I made that hash table called arr, so what I did is to come to every single digit in number and increment the position of that digit in arr

for example , number 554 will cause arr[5]++; twice and arr[4]++; only once , simple idea of using hash tables.

and then at the end I iterate over the hash array printing the number of occurrence of each digit in each number.

and here is the code :

#include <stdio.h>
#include <math.h>
int main()
{
    int arr[6] = {5555, 12112, 14, -3223, 44, 10001};

    int tempArr[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

    for (int i = 0; i < 6; i++) {
        int temp1 = arr[i];

        // get the number of occurrences of each digit in each number
        do{
            tempArr[(abs(temp1) % 10)]++;
            temp1 /= 10;
        }while(temp1 != 0);

        // loop over the array to know how many times each digit occurred
        printf("the number of occurrences in number called %d\n", arr[i]);
        for (int j = 0; j < 10; j++) {
            if(tempArr[j] > 1)
                printf("number %d occurred %d times\n", j, tempArr[j]);

            // resetting that position of the array
            tempArr[j] = 0;
        }

    }
    return 0;
}

and here is the output :

the number of occurrences in number called 5555
number 5 occurred 4 times
the number of occurrences in number called 12112
number 1 occurred 3 times
number 2 occurred 2 times
the number of occurrences in number called 14
the number of occurrences in number called -3223
number 2 occurred 2 times
number 3 occurred 2 times
the number of occurrences in number called 44
number 4 occurred 2 times
the number of occurrences in number called 10001
number 0 occurred 3 times
number 1 occurred 2 times

Upvotes: 1

Jonathan Fiore
Jonathan Fiore

Reputation: 21

You can divide your number multiple times by 10:

int number = 72;
int rest;

while(number)
{
    rest = number % 10;
    printf("%d\n", rest);
    number /= 10;
}

Here rest contains '2' and then '7'

Upvotes: 2

Related Questions