Checking weather a digit exist in a number or not

Problem Statement
 Write a C function checkdigit which takes as input an integer
x, and a number dig (an integer between 0 and 9), and checks
if the number x has dig as one of its digits. the function
returns 1 if the digit dig occurs in x, 0 otherwise.
(b) Write a C function testdigits that takes as input two integers
x and y, and returns 1 if all digits of y occur as digits on x, 0
otherwise. This function should be implemented by calling
checkdigit.
(c) Write a main program that accepts two numbers, a and b
from the user, and checks if all the digits of a occur in b.


#include<stdio.h>
int cheakdigit(int w,int y);
int testdigit(int t,int y);
int main()
{
  int s,y,x,dig,d;
  printf("enter x\n");
  scanf("%d",&x);
  printf("enter a digit \n");
  scanf("%d",&dig);
  if(dig>9)
    {
      printf("unable to proceed \n enter a single digit <9 \n");
    }
  else
    {
      s=cheakdigit(x,dig);
      if(s==0)
{
 printf("%d does not appear in %d \n",dig,x);
}
      else
{
 printf("%d appears in %d \n",dig,x);
}
    }
  printf("enter y \n");
  scanf("%d",&y);
  d=testdigit(x,y);
  if(d==1)
    {
      printf("all digits of x appears in the number y\n");
    }
  else
    {
      printf("all digits of x does not appear in y \n");
    }
  return 0;
}
int cheakdigit(int a,int b)
{
  do
    {
      if(a%10==b)
{
 return 1;
}
      a=a/10;
    }while(a%10>=1);
  return 0;
}
int testdigit(int a,int b)
{
  int c[10],x[10],i,j,temp,temp2;
  i=j=temp=temp2=0;
  while(a%10>=1)
    {
      c[i]=a%10;
      i++;
      a=a/10;
    }
  while(b%10>=1)
    {
      x[j]=b%10;
      b=b/10;
      j++;
    }
  while(temp<j)
    {
      temp2=0;
      while(temp2<i&&c[temp2]!=x[temp])
{
 temp2++;
}
      if(c[temp2]==x[temp])
{
 temp++;
}
      else
{
 return 0;
}
    }
  return 1;
}

No comments:

Post a Comment