Saturday, February 11, 2017

Write a Program in Java to input a number and check whether it is a Keith Number or not.

Note:A Keith Number is an integer N with ‘d’ digits with the following property:
If a Fibonacci-like sequence (in which each term in the sequence is the sum of the ‘d’ previous terms)
is formed, with the first ‘d’ terms being the decimal digits of the number N, then N itself occurs as a
term in the sequence.

For example, 197 is a Keith number since it generates the sequence
1, 9, 7, 17, 33, 57, 107, 197, ………..

Some keith numbers are: 14 ,19, 28 , 47 , 61, 75, 197, 742, 1104, 1537……………   */

import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import static java.lang.System.out;
class Keith
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
out.print("Enter a No : ");
String s=br.readLine();
int n=Integer.parseInt(s);
int temp=n;
int i=s.length();
int[] arr=new int[50];
while(n!=0)
{
arr[i-1]=n%10;
n=n/10;
i--;
}
int sum=0;i=s.length();
while(sum<temp)
{
sum=0;
for(int j=1;j<=s.length();j++)
sum+=arr[i-j];
arr[i]=sum;
i++;
}
if(sum==temp)
out.println(temp+" is a keith no");
else
out.println(temp+" is a not keith no");
}
}

Share this

0 Comment to "Write a Program in Java to input a number and check whether it is a Keith Number or not."

Post a Comment