Keith Number in Java3 min read

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

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.

Output:

Enter a number: 19
19 is a Keith number.


MORE

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …