Home > other >  How to iterate over numbers in C
How to iterate over numbers in C

Time:09-09

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?

CodePudding user response:

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

CodePudding user response:

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'

CodePudding user response:

Here is a very minor riff 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> // 'memset()'
#include <limits.h> // 'INT_MIN/MAX'

int my_main() {
    int tests[] = { 0, 11, 121, 5555, 12112, 14, -3223, 44, 1223334444, INT_MIN, INT_MAX };

    // run through multiple test values
    for( int i = 0; i < sizeof tests/sizeof tests[0]; i   ) {
        // counter for each digit, all init'd to zero
        int cnts[ 10 ];
        memset( cnts, 0, sizeof cnts );

        int test = tests[ i ];
        // count occurences of each digit (right to left)
        for( ; test;test /= 10 )
            cnts[ abs(test % 10) ]  ;

        // report only digits appearing multiple times
        printf( "d: ", tests[ i ] );
        char *sep = "";
        for( int j = 0; j < 10; j   )
            if( cnts[ j ] > 1 )
                printf( "%s%dx%d", sep, cnts[ j ], j ), sep = ", ";

        if( !*sep )
                printf( "--" );

        putchar( '\n' );
    }
    return 0;
}

Output

          0: --
         11: 2x1
        121: 2x1
       5555: 4x5
      12112: 3x1, 2x2
         14: --
      -3223: 2x2, 2x3
         44: 2x4
 1223334444: 2x2, 3x3, 4x4
-2147483648: 3x4, 2x8
 2147483647: 3x4, 2x7

(Yes, an old 32-bit compiler.)

CodePudding user response:

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).

....... 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.

  • Related