user3451179 - 1 year ago 72
C Question

# how to find how many time a digit 0-9 appears in an integer

i need some help with a code, as the heahline is saying i need to find how many times a digit 0-9 appears in an integer and print it like so:
300
in a base of 10
0:2
1:0
2:0
3:1
4:0
5:0
6:0
7:0
8:0
9:0
i have try'ed

``````#include <stdio.h>
#include <stdlib.h>

int main()

{

int num, div=1, rem, count=0, i;
printf("Enter Number: ");
scanf("%d", &num);
div = num;
for(i=1; i<=10; i++)
{
div = num;
while(div!< 0)
{
rem = div % 10;
div = div / 10;

if(i == rem)
{
count++;
}

if(i == rem && count >= 2)
{
printf("\n%d is present %d times", i, count);
}
}
}
return 0;
}
``````

it returns the right answer but not the way i need. thanks for the help!

This code will give you count of number of each digit

``````#include <stdlib.h>
int main()
{
int num, div=1, rem,j,  i;
printf("Enter Number: ");
scanf("%d", &num);    //3002
div = num;
int count[10]={0,0,0,0,0,0,0,0,0,0};
while(div>0)
{

rem=div%10;
div=div/10;
printf("%d \n",rem);

for(i=0; i<=9; i++)
{

if(rem==i)
{
count[i]=count[i]+1;
}
}
}
for(j=0;j<=9;j++)
{
printf("%d",count[j]);}
return 0;
}
``````

If you enter number 3002, it will give output as 2011000000, indicating 2 0's, 1 two and 1 three.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download