AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-programming: Java Program to find given number is a Keith Number

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, ???.. and it contains the original number 197

Sample Input:

197

Sample Output

197 is a Keith number

Sample Input:

12

Sample Output:

12 is not a Keith number

corejava x 333
programming x 150
Posted On : 2017-12-07 17:34:20.0
profile Divesh - anyforum.in Divesh
8800
up-rate
4
down-rate

Answers


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import static java.lang.System.out;
class KeithNumber
{
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 number");
else
out.println(temp+" is not a Keith number");
}
}

Posted On : 2017-12-07 23:26:11
Satisfied : 1 Yes  0 No
profile anjali - anyforum.in anjali
050
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in