ISC Practicals and Theory - Keith Number (OOP)
Updated 20 Oct 2026
A Keith number is a number that appears in a series made from its own digits: start with the digits, then add the last few terms to get each new term. This version writes the check as a class KeithNumber with a parameterised constructor and a separate method for every job.
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 its digits and each later term is the sum of the previous $d$ terms.
A Keith number must have at least 2 digits, so a number below 10 is not tested: the program prints an invalid-input message for it.
Example: 75 has 2 digits, so the series is $7, 5, 12, 17, 29, 46, 75$. It reaches 75, so 75 is a Keith number. For 15 the series is $1, 5, 6, 11, 17, \ldots$ and skips 15, so 15 is not.
Algorithm
mainreads the number intonum, createsKeithNumber ob = new KeithNumber(num)and callsob.display().- The constructor stores
numin the data membern, setsd = 0and makesta one-placelongarray (it is resized later). display()prints the invalid message ifn < 10; otherwise it callsisKeith().isKeith()callscountDigits()to setd, thenstoreDigits()to fill the arraytwith the digits ofn.- While
next < n:sumOfTerms()returns the sum of thedterms int; this is the newnextshift(next)moves every term one place left and putsnextat the end
- When the loop ends, the method returns
next == n. display()prints the result.
The terms (t, next, s and term) are long. When n is close to 2147483647 the sum of d terms can be larger than the biggest int; with int it would wrap round to a negative number and the loop would never end.
Program
Each method does one job: countDigits() counts, storeDigits() fills the array, sumOfTerms() adds, shift() slides the window and isKeith() controls the series.
import java.util.*;
/**
* Class KeithNumber (OOP version) checks whether a number is a Keith number.
* The object is created with the number; each method does one job.
*/
public class KeithNumber
{
int n; // the number to be checked
int d; // number of digits in n
long t[]; // the latest d terms of the Keith series (long: sums can exceed the biggest int)
/**
* Parameterised constructor: stores the number and prepares the array.
* @param num the number to be checked
*/
KeithNumber(int num)
{
n = num;
d = 0;
t = new long[1]; // resized once the digit count is known
}
/** Counts the digits of n and stores the count in d. */
void countDigits()
{
int x = n; // working copy of n
d = 0;
while (x > 0)
{
d++;
x = x / 10; // drop the last digit
}
}
/** Stores the digits of n, left to right, as the first d terms in t[]. */
void storeDigits()
{
t = new long[d];
int x = n; // working copy of n
for (int i = d - 1; i >= 0; i--)
{
t[i] = x % 10; // last digit goes to the last place
x = x / 10;
}
}
/**
* Adds up the d terms currently held in t[].
* @return the sum of the d terms
*/
long sumOfTerms()
{
long s = 0; // running sum
for (int i = 0; i < d; i++)
s = s + t[i];
return s;
}
/**
* Slides the window of terms one place to the left and adds the new term.
* @param term the newly generated term of the series
*/
void shift(long term)
{
for (int i = 0; i < d - 1; i++)
t[i] = t[i + 1]; // the oldest term is dropped
t[d - 1] = term; // the new term goes at the end
}
/**
* Generates the series until a term is not less than n.
* @return true if the series reaches n exactly, otherwise false
*/
boolean isKeith()
{
countDigits();
storeDigits();
long next = 0; // the term being generated
while (next < n)
{
next = sumOfTerms();
shift(next);
}
return next == n;
}
/** Displays the result for the number. */
void display()
{
if (n < 10)
System.out.println("Invalid input: a Keith number must have at least 2 digits");
else if (isKeith())
System.out.println(n + " is a Keith number");
else
System.out.println(n + " is not a Keith number");
}
/** main method: reads the number, creates the object and calls display(). */
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
KeithNumber ob = new KeithNumber(num);
ob.display();
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
n | int | Data member: the number to be checked |
d | int | Data member: number of digits in n (length of the window) |
t | long[] | Data member: the latest d terms of the series |
num (in constructor) | int | Parameter: the number passed to the constructor |
x (in countDigits, storeDigits) | int | Working copy of n used to extract digits |
i | int | Loop counter in storeDigits(), sumOfTerms() and shift() |
s | long | Running sum in sumOfTerms() |
term | long | Parameter of shift(): the new term to place at the end |
next | long | The term being generated in isKeith() |
sc | Scanner | Reads the number in main |
num (in main) | int | The number typed by the user |
ob | KeithNumber | Object created in main |
args | String[] | Command-line arguments of main (not used) |
Sample input and output
Sample 1
Enter a number: 75
75 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: 75, so d = 2 and t = {7, 5}.
| Step | next | t[0] | t[1] | next < n |
|---|---|---|---|---|
| Start | 0 | 7 | 5 | true |
| Pass 1 | 12 | 5 | 12 | true |
| Pass 2 | 17 | 12 | 17 | true |
| Pass 3 | 29 | 17 | 29 | true |
| Pass 4 | 46 | 29 | 46 | true |
| Pass 5 | 75 | 46 | 75 | false |
next == n (75 == 75), so isKeith() returns true and the program prints 75 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."