Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,987 questions

51,931 answers

573 users

How to find the number of occurrences (frequency) of each digit in a number in C

1 Answer

0 votes
#include <stdio.h>

void digitsCounter(int n, int arr[]) {
	while (n) {
        arr[n % 10]++;
		n /= 10;
    }
}
  
int main()
{
	int arr[10] = {0}, n = 79712622;
	
    digitsCounter(n, arr);
	for (int i = 0; i < 10; i++) {
        if (arr[i] != 0)
			printf("digit:%d - %d times\n", i, arr[i]);
	}
       
    return 0;
}
  
  
  
/*
run:
  
digit:1 - 1 times
digit:2 - 3 times
digit:6 - 1 times
digit:7 - 2 times
digit:9 - 1 times
    
*/

 



answered Jul 4, 2020 by avibootz
...