- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# How To Check Whether a Number is a Keith Number or Not in Java?

A number is said to be a Keith number, if it is arranged with a special sequence of numbers which are initially created by its digits.

After that the numbers of the sequence will be generated by adding its previous numbers till the number is exceeded from the input number. The results will be decided if the end number is the same as input number.

While adding the previous numbers during generating sequence remember to add nth numbers only where n refers to the number of digits in the original input number.

In this article we will see how to check if a number is a Keith number by using Java programming language.

## To show you some instances

### Instance-1

Input number is 19

Let’s check it by using the logic of Keith number −

By separating the digits of 19 we get our first sequence= 1, 9.

Now add all the digits= 1 + 9 = 10.

Now the new sequence is 1, 9, 10.

Now add the last two numbers = 9 + 10 = 19.

So the new sequence is 1, 9, 10, 19.

Here we can see the last number of the sequence is the same as our input number or the origin number.

Hence, 19 is a Keith number.

### Instance-2

Input number is 197

Let’s check it by using the logic of Keith number −

By separating the digits of 197 we get our first sequence= 1, 9, 7.

Now add all the digits= 1 + 9 + 7 = 17.

Now the new sequence is 1, 9, 7, 17.

Now add the last three numbers = 9 + 7 + 17 = 33.

The new sequence is 1, 9, 7, 17, 33.

Now add the last three numbers = 7 + 17 + 33 = 57.

The new sequence is 1, 9, 7, 17, 33, 57

If we follow the same steps, we will get the sequence = 1, 9, 7, 17, 33, 57, 107, 197.

Here we can see the last number of the sequence is the same as our input number or the origin number.

Hence, 197 is a Keith number

### Instance-3

Input number is 152

Let’s check it by using the logic of Keith number −

By separating the digits of 152 we get our first sequence= 1, 5, 2.

Now add all the digits= 1 + 5 + 2 = 8.

Now the new sequence is 1, 5, 2, 8.

Now add the last three numbers = 5 + 2 + 8 = 15.

The new sequence is 1, 5, 2, 8, 15.

Now add the last three numbers = 2 + 8 + 15 = 25.

The new sequence is 1, 5, 2, 8, 15, 25.

If we follow the same steps, we will get the sequence = 1, 5, 2, 8, 15, 25, 48, 88, 161.

Here we can see the last number of the sequence is exceeding our input number or the origin number.

Hence, 152 is not a Keith number.

**Some other examples of Keith numbers include 19, 197, 742, 1537, etc.**

## Algorithm

**Step 1**− Get an integer number either by initialization or by user input.**Step 2**− Declare an array for storing the calculated numbers.**Step 3**− Initially find the digits of the input number.**Step 4**− Then with the use of our algorithm inside the looping we calculated the next values till the value exceeding the input number.**Step 5**− Finally, we are comparing the last calculated number with the input number, if both are same then we will reach the conclusion that the input number is Keith number otherwise the input number is not the Keith number.

## Approach

We have provided the solution in different approaches.

By Using Static Input Value

Let’s see the program along with its output one by one.

### Approach: By Using Static Input Value

In this approach, the user will be asked to enter the input number and then by using the algorithm we can check whether the number is a Keith number or not.

#### Example

public class Main{ //main method public static void main(String[] args){ //declared an integer variable and initialized the value int inputNumber = 14; //print the given input number System.out.println("Given number: "+inputNumber); //store it to an another temporary variable int temporaryNumber = inputNumber; //type casting it to string String str = Integer.toString(inputNumber); //find the length and store the length value into a variable int len =str.length(); //declare a array which store the input number int store[]=new int[inputNumber]; int i, sum; //initiate the looping for break the input number into single digits for(i=len-1; i>=0; i--){ // store the digits into the same array store[i]=temporaryNumber % 10; temporaryNumber = temporaryNumber/10; } i=len; sum=0; //start iteration for calculating the next numbers while(sum<inputNumber){ sum = 0; for(int j=1; j<=len; j++){ sum=sum+store[i-j]; } //store the calculated numbers into the array store[i]=sum; i++; } //check the resultant number is matched to the input number or not if(sum==inputNumber) System.out.println(inputNumber + " is a Keith Number."); else System.out.println(inputNumber + " is not a Keith Number."); } }

#### Output

Given number: 14 14 is a Keith Number.

In this article, we explored how to check a number whether it is a Keith number or not in Java by using different approaches.