Miscellaneous
– II – Keith No.
A Keith
Number is an n-digit integer number 'N'.
It create a Fibonacci-like sequence.
1.
Separate the digit of 'N' sum it.
2.
Replace the first digit with second and so on.
3.
Include the sum at the last position.
4. Sum the
new set.
5.
Continue the proces 2-4 till sum is less than 'N'.
6. If
final sum comes equals to the 'N' then it is known as Keith Number.
Ex. 1.
14
1 + 4 =
5
4 + 5
=9
5 + 9
=14
Ex. 2.
197
1 + 9
+ 7 = 17
9 + 7
+ 17 = 33
7 + 17
+ 33 = 57
17 + 33
+ 57 = 107
33 + 57
+ 107 = 197
Some
other Keith Numbers : 19, 28, 47, 61, 75, 742, 1104 1537 .
//Checking if a
given number is a Vampire Number or not
#include <stdio.h>
#include <conio.h>
void
print(int a[],int dc)
{
int
i;
for(i=0;i<dc;i++)
{
if(i<dc-1)
{
printf("%d
+ ",a[i]);
}
else
{
printf("%d
",a[i]);
}
}
}
int
digit(int n,int a[],int dc)
{
int
i=dc-1,s=0;
while(n>0)
{
a[i--]=n%10;
s=s+n%10;
n=n/10;
}
printf("\n\t\t");
print(a,dc);
printf("
= %d",s);
printf("\n\t\t");
return
s;
}
int
dCount(int n)
{
int
c=0;
while(n>0)
{
c++;
n=n/10;
}
return
c;
}
void
shift(int a[],int s,int dc)
{
int i;
for(i=0;i<dc-1;i++)
{
a[i]=a[i+1];
}
a[dc-1]=s;
}
int
sumAr(int a[],int dc)
{
int
i,s=0;
for(i=0;i<dc;i++)
{
s=s+a[i];
}
return
s;
}
void
main()
{
int
i,dc,sum=0,n,*a;
clrscr();
printf("\n\tEnter a
Number : ");
scanf("%d",&n);
sum=0;
dc=dCount(n);
a=(int
*)malloc(dc*sizeof(int));
sum=digit(n,a,dc);
while(sum<n)
{
shift(a,sum,dc);
sum=sumAr(a,dc);
printf("\n\t\t");
print(a,dc);
printf(" =
%d",sum);
printf("\n\t\t");
}
if(sum==n)
{
printf("\n\n\t\t%d
is a Keith Number",n);
}
else
{
printf("\n\n\t\t%d
is not a Keith Number",n);
}
getch();
}
No comments:
Post a Comment