Problem statement
This program take any number from user and check it is self-descriptive number or not.
What is self-descriptive number?
A number is called as self-descriptive when the position of digit represents the number of time it appears in that number.
There are so many numbers are self-descriptive.
For example:
1 |
1210, 2020, 21200, 3211000, 42101000 etc |
Consider 2020,
- position 0 has value 2 and there are two 0s in the number;
- position 1 has value 0 and there are no 1s in the number;
- position 2 has value 2 and there are two 2s in the number;
- position 3 has value 0 and there are zero 3s in the number.
Now consider a self-descriptive number 6210001000,
- contains 6 at position 0, indicating that there are six 0s in 6210001000;
- It contains 2 at position 1, indicating that there are two 1s in 6210001000;
- It contains 1 at position 2, indicating that there is one 2 in 6210001000;
- It contains 0 at position 3, indicating that there is no 3 in 6210001000;
- It contains 0 at position 4, indicating that there is no 4 in 6210001000;
- It contains 0 at position 5, indicating that there is no 5 in 6210001000;
- It contains 1 at position 6, indicating that there is one 6 in 6210001000;
- It contains 0 at position 7, indicating that there is no 7 in 6210001000;
- It contains 0 at position 8, indicating that there is no 8 in 6210001000;
- It contains 0 at position 9, indicating that there is no 9 in 6210001000.
Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
#include<stdio.h> int check(int,int); int getNumDigits(int); int main() { int num=0,temp=0, digit=0,count=0; printf("enter the number\n"); scanf("%d",&num); temp=num; int flag = 1; int numDigit = getNumDigits(temp); while(temp>0) { digit=temp%10; int count=check(num,numDigit); if(count!= digit) { printf("\tNumber is not a self descriptive"); flag = 0; break; } temp=temp/10; numDigit--; } if(flag) { printf("\tNumber is a self descriptive"); } } /* check number of times the digit appear in number */ int check(int num,int digit) { int count=0; while(num>0) { if(num%10 == digit) { count++; } num=num/10; } return count; } /* to check number of digits in number */ int getNumDigits(int num) { printf("%d",num); int digits = 0; while(num>0) { digits++; num=num/10; } return digits-1; } |
Output
1 2 |
enter the number 3211000 Number is a self descriptive |
#include
int main()
{
char str[]=”2020″;
int len=strlen(str);
printf(“%d”,fun(str,len));
return 0;
}
int fun(char str[],int n)
{
int arr[n],freq_arr[n];
int index=0,flag=0;
for(int i=0; i<n; i++)
arr[index++]=(str[i]-'0');
index_frequency(arr,freq_arr,n);
for(int i=0; i<n; i++)
if(freq_arr[i]!=arr[i])
{
flag=1;
break;
}
if(flag==1)
printf("Not Autobiographical Number\n");
else
printf("Auto biographical Number\n");
return;
}
void index_frequency(int arr[],int freq_arr[],int n)
{
int count;
for(int i=0; i<n; i++)
{
count=0;
for(int j=0; j<n; j++)
if(i==arr[j])
count++;
freq_arr[i]=count;
}
}