ISC Practicals and Theory - Keith Number
Updated 21 Oct 2026
A Keith number is a number that shows up inside a series built from its own digits. You start the series with the digits of the number, then keep adding the last few terms to get the next one. If the series lands exactly on the number, it is a Keith number. This note gives the method, an ISC-style Java program written in the main method alone, sample runs and a dry run.
Definition
A number $N$ with $d$ digits ($d \ge 2$) is a Keith number if it appears in the series whose first $d$ terms are the digits of $N$ and in which every later term is the sum of the previous $d$ terms.
A Keith number must have at least 2 digits. A number below 10 is not tested: the program prints an invalid-input message for it.
Example: 14 has 2 digits, so each term is the sum of the previous 2 terms.
$1, 4, 5, 9, 14, \ldots$
14 appears, so 14 is a Keith number.
Example: 197 has 3 digits, so each term is the sum of the previous 3 terms.
$1, 9, 7, 17, 33, 57, 107, 197, \ldots$
197 appears, so 197 is a Keith number.
Example: 15 gives $1, 5, 6, 11, 17, \ldots$. The series jumps from 11 to 17 and never equals 15, so 15 is not a Keith number.
Algorithm
- Accept the number
n. Ifn < 10, print an invalid-input message and stop, because a Keith number must have at least 2 digits. - Count the digits of
nind: copynintoxand, whilex > 0, add 1 todand drop the last digit withx = x / 10. - Create the array
tof sizedto hold the latestdterms of the series. Fill it with the digits ofn, the last digit going in the last place (t[i] = x % 10, forifromd - 1down to 0). - Set
next = 0. Whilenext < n:- set
nextto the sum of all thedterms int; - slide the window one place to the left (
t[i] = t[i + 1]) and putnextin the last placet[d - 1].
- set
- When the loop stops,
nextis the first term that is not less thann. Ifnext == n, the series hitsnexactly, sonis a Keith number. Otherwise it is not.
Program
Everything is written inside main: the digit count, the array of terms and the while loop that generates the series.
import java.util.*;
public class Keith
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt(); // the number to be checked
if (n < 10)
{
System.out.println("Invalid input: a Keith number must have at least 2 digits");
}
else
{
int d = 0; // number of digits in n
int x = n; // working copy of n
while (x > 0)
{
d++;
x = x / 10; // drop the last digit
}
long t[] = new long[d]; // the latest d terms of the series
x = n;
for (int i = d - 1; i >= 0; i--)
{
t[i] = x % 10; // first d terms are the digits of n
x = x / 10;
}
long next = 0; // the term being generated
while (next < n)
{
next = 0;
for (int i = 0; i < d; i++)
next = next + t[i]; // sum of the previous d terms
for (int i = 0; i < d - 1; i++)
t[i] = t[i + 1]; // slide the window one place left
t[d - 1] = next; // newest term goes at the end
}
if (next == n) // n is Keith if the series hits n exactly
System.out.println(n + " is a Keith number");
else
System.out.println(n + " is not a Keith number");
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the input typed by the user |
n | int | The number to be checked |
d | int | Number of digits in n |
x | int | Working copy of n whose last digit is dropped, first to count and then to fill the array |
t | long[] | The latest d terms of the series |
i | int | Loop counter used when filling the array, adding the terms and sliding the window |
next | long | The term being generated, the sum of the previous d terms |
Sample input and output
Sample 1
Enter a number: 14
14 is a Keith numberSample 2
Enter a number: 197
197 is a Keith numberSample 3
Enter a number: 15
15 is not a Keith numberSample 4
Enter a number: 7
Invalid input: a Keith number must have at least 2 digitsDry run
Input: 14. Here d = 2 and t starts as the digits of 14.
| Step | next | t[0] | t[1] | next < n |
|---|---|---|---|---|
| Start | 0 | 1 | 4 | true (0 < 14) |
| Pass 1 | 1 + 4 = 5 | 4 | 5 | true (5 < 14) |
| Pass 2 | 4 + 5 = 9 | 5 | 9 | true (9 < 14) |
| Pass 3 | 5 + 9 = 14 | 9 | 14 | false (14 < 14) |
The loop stops with next = 14, and next == n, so the program prints 14 is a Keith number.
Variations
- Range of Keith numbers: "Display all Keith numbers between two given limits m and n."
- Smith number: "Check whether a number is a Smith number."
- Disarium number: "Check whether a number is a Disarium number."