To check whether the given number is mono-digit or not

To check whether the given number is mono-digit or not

Question:

Write a program to check the number is mono-digit or not.

Conditions:

  1. The programmer is allowed to add and subtract the consecutive digits (starting from left).
  2. And you are allowed to do only one operation on a digit.
  3. You cannot perform any operation on a resultant digit of the previous operation.
  4. Your code should also find if a given number cannot be converted to a mono digit number.

Logic:

  • Get the input as a string and convert it into an integer array.
  • Then check for three possibilities for a mono digit to occur.
  1. The first element is a mono digit.
  2. Addition of first and second element is a mono digit.
  3. Subtraction of first and second element is a mono digit.
  • These possibilities calculated and stored in c[0].
  • And check the conditions as we mentioned above:
  1.  A single digit is equal to c[0].
  2.  Addition of two consecutive digits is equal to c[0].
  3.  Subtraction of two consecutive digits is equal to c[0].
  • If any mono digit occurs means print which is stored in array c.
  • And also shows the occurrence non-mono digit.

Program:

#include<stdio.h>
int main()
{
    int i,j=0,b[20],k=0,c[20],m=1,n=0,l=0;
    char a[20];
    printf("enter the number:");
    gets(a);                     //get the input as a string
    i=strlen(a);                 //length of string
    while(a[j]!='\0')            //convert the string to integer
    {
        b[j]=a[j]-48;            //stores converted value into integer array
        j++;
    }
    for(k=0;k<3;k++)             //check three possibilities
    {
        if(k==0)                 //checks with first element
        {
            c[0]=b[0];
            m=1,n=0;
        }
        else if(k==2)           //checks with addition of first and second element
        {
            c[0]=b[0]+b[1];
            m=2,n=0;
        }
        else                   //checks with subtraction of first and second element
        {
            c[0]=b[0]-b[1];
            m=2,n=0;
        }
        check:                 //check is the label
        for(j=m;j<i;j++)
        {
            n++; //increments the monodigit array position
            if(c[0]==b[j]) //single digit is equal to c[0]
            {
                c[n]=b[j]; //store the monodigit value into array
                m=j+1; //increments the index by one 
                goto check;
            }
            else if(c[0]==b[j]+b[j+1]) //addition of two consecutive are equal to c[0]
            {
                c[n]=b[j]+b[j+1];
                m=j+2;
                goto check;
            }
            else if(c[0]==b[j]-b[j+1]) //subtraction of two consecutive are equal to c[0]
            {
                c[n]=b[j]-b[j+1];
                m=j+2;
                goto check;
            }
            else //breaks from loop when all conditions fails
            {
                l++;
                break;
            }
        }
        if(m>i-1)                     //prints the monodigit
        {
            printf("\nGiven number is monodigit:");
            for(j=0;j<n+1;j++)
            {
                printf("%d ",c[j]);
            }
        }
    }
    if(l==3)                         //prints when three possibilities fails
    {
        printf("It is not monodigit");
    }
    return 0;
}

You might also like:

Sum Of Digits Of A Number Until The Sum Is A Single Digit

 

 

Follow For Instant Updates

Join WhatsApp Group: link
Join our Telegram Channel: link
Like our Facebook Page:  link
Subscribe to our Youtube channel: link

Vignesh

A Computer Science graduate who likes to make things simpler. When he’s not working, you can find him surfing the web, learning facts, tricks and life hacks. He also enjoys movies in his leisure time.
0 0 votes
Article Rating
Subscribe
Notify of
guest
5.4K Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
5.4K
0
Would love your thoughts, please comment.x
()
x