w3resource

Java: Check whether a number is a Keith Number or not


Check Keith Number

Write a Java program to check whether a number is a Keith Number or not.

In recreational mathematics, a Keith number or repfigit number (short for repetitive Fibonacci-like digit) is a number in the following integer sequence:
14, 19, 28, 47, 61, 75, 197, 742, 1104, 1537, 2208, 2580, 3684, 4788, 7385, 7647, 7909, 31331, 34285, 34348, 55604, 62662, 86935, 93993, 120284, 129106, 147640, 156146, 174680, 183186, 298320, 355419, 694280, 925993,

Test Data
Input a number: 75

Sample Solution:

Java Code:

import java.util.Scanner;
public class Example26  {

   public static void main(String[] args) { 

     Scanner sc = new Scanner( System.in ); 
     System.out.print("Input a number: "); 
     int n = sc.nextInt(); 
     int n1 = n;
     String s = Integer.toString(n);
     int d=s.length();
     int arr[]=new int[n];
     int i, sum; 
     for(i=d-1; i>=0; i--)
     {
         arr[i]=n1 % 10;
         n1=n1/10;
          
     }
      
     i=d; sum=0;
     while(sum<n)
     {
         sum = 0;
         for(int j=1; j<=d; j++)
         {
             sum=sum+arr[i-j];
         }
         arr[i]=sum;
         i++;
     }
 
    if(sum==n)
        System.out.println("Keith Number");
     else
        System.out.println("Not a Keith Number");
    }
}

Sample Output:

Input a number: 75                                                                                        
Keith Number 

Flowchart:

Flowchart: Check whether a number is a Keith Number or not


For more Practice: Solve these Related Problems:

  • Write a Java program to verify if a number is a Keith number by generating its digit sequence until the number is reached or exceeded.
  • Write a Java program to generate all Keith numbers within a specified range using iterative loops.
  • Write a Java program to implement a Keith number checker using a queue to simulate the digit sequence.
  • Write a Java program to optimize Keith number detection by caching intermediate sequence sums.

Go to:


PREV : First 15 Pell Numbers.
NEXT : First 20 Hamming Numbers.

Java Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.