Friday, 17 October 2014

Finding the given number is a self number or not


Question - Finding the given number is a self number or not  .


Solution -


self number is an integer that cannot be written as the sum of any another integer n and the individual digits of n. This property is specific to the base used to represent the integers. 20 is a self number (in base 10), because no such combination can be found (all n < 15 give a result < 20; all other n give a result > 20). 21 is not, because it can be written as 15 + 1 + 5 using n = 15.


Source:Wikipedia 



Here is how we write code for this :->


/* programs tells whether input  no is self number or not  */

#include<stdio.h>
#include<stdio.h>
int main ()
{
int num,arr[10],k,j,i;
printf("Enter any number\n");
scanf("%d",&num);
div(num);
}
div(int z)
{
int i,p,k,j,a;

    for(i=0;i<z;i++)
    {
    a=i;
    p=0;
         while(a!=0)
        { 
        p=p+a%10;
        a=a/10;
        }     

            k=p+i;
            if(k==z)
         { 
        printf("Number is not the self number\nsince %d + %d =%d\n",i,p,z);
        break;
        }
        else
         {
        if(i==z-1)
        printf("It's a self number\n");
        }



    }


}



No comments:

Post a Comment