Keith Number
✹ A Keith Number if it forms a Fibonacci-like sequence that begins with the digits of the number itself. The most significant digit is followed by the rest of the digits. Now each subsequent term is the sum of the previous n terms, and the number N itself appears in this sequence.
For Example: 197.
1 + 9 + 7 = 17
9 + 7 + 17 = 33
7 + 17 + 33 = 57
17 + 33 + 57 = 107
33 + 57 + 107 = 197 (the number itself)
14, 19, 28, 47, 61, 75, 197, 742, 1104, 1537, 2208, 2580, … are some of the Keith Numbers.
import java.util.*;
class Keith_Digit
{
public static void main(String args[])
{
int i,j,n,m,d=0,s,arr[];
Scanner sc= new Scanner(System.in);
System.out.println("Enter the Number = ");
n=sc.nextInt();
m=n;
//Count Number of Digit
while(m>0)
{
d++;
m/=10;
}
arr=new int [d+1];
j=d-1;
for(i=n;i>0;i=i/10)
{
arr[j--]=i%10;
}
while (arr[0]<n)
{
System.out.print(arr[0]+", ");
s=0;
for(i=0;i<d;i++)
s+=arr[i];
arr[d]=s;
for(i=0;i<d;i++)
arr[i]=arr[i+1];
}
if(arr[0]==n)
System.out.println("\nThe Number is Keith Number");
else
System.out.println("\nThe Number is Not Keith Number");
}
}
class Keith_Digit
{
public static void main(String args[])
{
int i,j,n,m,d=0,s,arr[];
Scanner sc= new Scanner(System.in);
System.out.println("Enter the Number = ");
n=sc.nextInt();
m=n;
//Count Number of Digit
while(m>0)
{
d++;
m/=10;
}
arr=new int [d+1];
j=d-1;
for(i=n;i>0;i=i/10)
{
arr[j--]=i%10;
}
while (arr[0]<n)
{
System.out.print(arr[0]+", ");
s=0;
for(i=0;i<d;i++)
s+=arr[i];
arr[d]=s;
for(i=0;i<d;i++)
arr[i]=arr[i+1];
}
if(arr[0]==n)
System.out.println("\nThe Number is Keith Number");
else
System.out.println("\nThe Number is Not Keith Number");
}
}
No comments