In this java programming tutorial, you will learn about Keith Number and its implementation in java. It is frequently asked in Java coding.

What is Keith Number?

A positive n digit number x is called a Keith number or repfigit number (repetitive Fibonacci-like digit) if it is arranged in a special number sequence generated using its digits. This sequence has n terms as digit of x and the next term is determined by the sum of previous n terms and so on. And if the number x falls under this sequence then x is a Keith Number. Example: 19, 742, etc

Explanation: Consider the number 742. Go through the following steps:

First separate the digit, 7, 4, 2.

To find the next term, add the digits, 7+4+2 = 13
New Series: 7, 4, 2, 13.

To find the next term, add the last three digits of the above sequence, 13+2+4=19
New Series: 7, 4, 2, 13, 19.

To find the next term, add the last three digits of the above sequence, 19+13+2=34
New Series: 7, 4, 2, 13, 19, 34.

To find the next term, add the last three digits of the above sequence, 34+19+13=66
New Series: 7, 4, 2, 13, 19, 34, 66.

To find the next term, add the last three digits of the above sequence, 66+34+19=119
New Series: 7, 4, 2, 13, 19, 34, 66, 119.

To find the next term, add the last three digits of the above sequence, 119+66+34=219
New Series: 7, 4, 2, 13, 19, 34, 66, 119, 219.

To find the next term, add the last three digits of the above sequence, 219+119+66=404
New Series: 7, 4, 2, 13, 19, 34, 66, 119, 219, 404.

To find the next term, add the last three digits of the above sequence, 404+219+119=742
New Series: 7, 4, 2, 13, 19, 34, 66, 119, 219, 404, 742.

Now we will stop as we got the number 742 as the term of the series. Hence 742 is a Keith Number.


What are the Steps to Find Keith Number with programming

  1. Read a number (x).
  2. Separate each digit from the number (x).
  3. Add all the separated n-digits and that will give the next term of the series.
  4. Again, add the last n-terms of the series to find the next term.
  5. Repeat step 4 until the term which the same as the number(x).

Java Program to check whether the Number is Keith Number or not

import java.util.Scanner;

public class KeithNumber
{
  public static void main(String[] args)
  {
    int i, sum = 0, n, num;
    Scanner sc = new Scanner(System.in);

    System.out.print("Enter a number: ");
    num = sc.nextInt();

    n = num;

    //string to get the length
    String s = Integer.toString(num);
    int d = s.length();
    int arr[] = new int[num];

    //separating the digit and storing it in array
    for (i = d - 1; i >= 0; i--)
    {
      arr[i] = n % 10;
      n = n / 10;

    }

    i = d;

    /*once the sum value becomes equal or greater than
    that of the entered number, it stops */
    while (sum < num)
    {
      sum = 0;
      for (int j = 1; j <= d; j++)
      {
        sum = sum + arr[i - j];
      }

      arr[i] = sum;
      i++;
    }

    //compares the sum with the entered number 
    if (sum == num)
      System.out.println(num + " is a Keith Number.");
    else
      System.out.println(num + " is not a Keith Number.");
  }
}

Output:

Enter a number: 742
742 is a Keith Number.

//Another output
Enter a number: 456
456 is not a Keith Number.


Another way to do it, by creating a function that will pass Boolean value to the main function.

import java.util.*;

public class KeithNumber
{
  //function that checks if the given number is Keith or not  
  static boolean isKeithNum(int num)
  {
    //array list to store the separated digit 
    ArrayList<Integer> terms = new ArrayList<Integer> ();

    int temp = num, n = 0;	//n = no. of digits  

    //condition to separate the digit 
    while (temp > 0)
    {
      terms.add(temp % 10);
      temp = temp / 10;

      n++;
    }

    //reverse the List  
    Collections.reverse(terms);
    int next = 0, i = n;

    //calculation of next terms
    while (next < num)
    {
      next = 0;

      //condition for adding the terms to get new term
      for (int j = 1; j <= n; j++)
        next = next + terms.get(i - j);

      terms.add(next);
      i++;
    }

    /*now the next is either equal or greater than entered number
    if equal then it is true else false*/
    return (next == num);
  }

  //main 
  public static void main(String[] args)
  {
    int number;
    Scanner sc = new Scanner(System.in);

    System.out.print("Enter a number: ");
    number = sc.nextInt();

    //passing the number as an argument to the function  
    if (isKeithNum(number))
      System.out.println(number + " is a Keith number.");
    else
      System.out.println(number + " is not a Keith number.");

  }
}

Output:

Enter a number: 19
19 is a Keith number.