C program to check number is self-descriptive number or not.

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:

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

Output

Ankit Wasankar

Senior Java Developer
Java 11 | Microservices | Reactjs | SQL

Help others by sharing the content!

1 thought on “C program to check number is self-descriptive number or not.”

  1. #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;
    }
    }

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.