A basic Keith Number Program
For example M = 197 is a keith number because, 197 has 3 digits, so n = 3 The number(197)appears in the special sequence that has first three terms as 1, 9, 7 and remaining terms evaluated using sum of previous 3 terms. 1, 9, 7, 17, 33, 57, 107, 197, …..
An n digit number M is called Keith number if it appears in a special sequence generated using its digits.
For example
M = 197 is a keith number because,
197 has 3 digits, so n = 3
The number(197)appears in the special sequence that has first three terms as 1, 9, 7 and remaining terms evaluated using sum of previous 3 terms.
1, 9, 7, 17, 33, 57, 107, 197, …..
For more number programs click the link
Unleashed: Important Number Programs for ICSE Part-1
Write a program to accept a number and print whether it is a keith number or not.
// Keith numbers: 14, 19, 28, 47, 61, 75, 197, 742, 1104 ...
import java.io.*;
class Keith //An n-digit number with value N is Keith if N is a part of Keith series generated
{
public static void main()throws IOException
{
System.out.println("Enter the number");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String num=br.readLine();
int len=num.length();
int nm=Integer.parseInt(num);
int arr[]=new int[len];
int n=nm;
for(int i=0;i<len;i++)
{
int d=n%10;
n/=10;
arr[len-1-i]=d;
}
for(int i=0;i<len;i++)
{
System.out.println(arr[i]);
}
int sum=0,count=0;
while(sum<nm)
{
sum=0;
for(int i=0;i<len;i++)
{
sum+=arr[i];
}
arr[count]=sum;
count++;
if(count>(len-1))
{
count=0;
}
}
if(sum==nm)
{
System.out.println("It is a Keith number");
}
else
{
System.out.println("Not a Keith number");
}
}
}Output:
Enter the number
197
It is a Keith number
To learn how to use Bluej to make java programs watch the video:
https://youtu.be/TRm9r4gFTho
Related Posts
Continue learning with these related articles

Logic Gates for ISC Class 11: Truth Tables, Universal Gates, Half and Full Adder
ISC Class 11 Computer Science notes on logic gates: AND, OR, NOT, NAND, NOR, XOR and XNOR with truth tables, why NAND and NOR are universal gates, and half adder and full adder circuits with their Boolean expressions.
Decimal to Binary Conversion in Java: 3 Programs with Examples
How to convert a decimal number to binary on paper and in Java: three programs (String, place value and Integer.toBinaryString), binary to decimal, a dry run and common mistakes.
Sum of First n Natural Numbers in Java (Loop and Formula)
Find the sum of the first n natural numbers in Java with a for loop, a while loop and the formula n(n + 1)/2, with a dry run and the sums of even numbers, odd numbers, squares and cubes.