
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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?
What is Keith Number?
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.
The number sequence will be generated by adding its previous numbers till the number exceeds the input number. The results will be decided if the end number is exactly the same as the input number.
For example, let's consider the number 14. The sequence will be as follows:
s1 = 1, 4 s2 = 1, 4, 1 + 4 s3 = 1, 4, 5, 4 + 5 s4 = 1, 4, 5, 9, 5 + 9
As you can see, the last number in the s4 sequence is 14 (5 + 9). Therefore, s14 is a Keith number.
Note: While adding the previous numbers while generating the sequence, remember to add the nth number only where n refers to the number of digits in the original input number.
Here are some other examples of Keith numbers such as: 19, 197, 742, 1537, etc.
Input & Output Scenarios
The following input and output scenarios will help you to check the Keith number by ding the mathematical calculation, which will help you program to implement the same logic:
Scenario 1
Suppose the given number is 19:
Input: 19 Output: Yes Calculation: 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, the 19 is a Keith number.
Scenario 2
Suppose the input number is 152:
Input: 152 Output: No Calculation: 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 that the last number of the sequence is the same as our input number or the origin number; the 152 is not a Keith number.
Example 1
The following program checks whether the given number 14 is a Keith number or not by determining whether the last number in the newly created sequence is equal to the given number:
public class checkKeith{ public static void main(String[] args){ int inputNumber = 14; System.out.println("The given number is: " + inputNumber); int temporaryNumber = inputNumber; //type casting it to string String str = Integer.toString(inputNumber); int len = str.length(); //declare a array which store the input number int store[] = new int[inputNumber]; int i; int sum = 0; //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; //start iteration for calculating the next numbers while(sum < inputNumber){ sum = 0; for(int j = 1; j <= len; j++){ sum += store[i-j]; } //store the calculated numbers into the array store[i] = sum; i++; } if(sum == inputNumber){ System.out.println("Yes! " + inputNumber + " is a Keith number."); } else{ System.out.println("No! " + inputNumber + " is not a Keith number."); } } }
Following is the output of the above program:
The given number is: 14 Yes! 14 is a Keith number.
Example 2
In the following example, we define a method named checkKeithNumber(), which creates a new sequence by adding the previous two numbers for the given number of digits and comparing the last digit with the number 22; if it is equal, then the number is a Keith number:
public class checkKeith { public static boolean checkKeithNumber(int num){ int temporaryNumber = num; //type casting it to string String str = Integer.toString(num); int len = str.length(); //declare a array which store the input number int store[] = new int[num]; int i; int sum = 0; //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; //start iteration for calculating the next numbers while(sum < num){ sum = 0; for(int j = 1; j <= len; j++){ sum += store[i-j]; } //store the calculated numbers into the array store[i] = sum; i++; } if(sum == num){ return true; } return false; } public static void main(String[] args){ int num = 22; System.out.println("The given number is : " + num); //calling the checkKeithNumber() for checking keith number System.out.println("Is the number " + num + " is keith number: " + checkKeithNumber(num)); } }
The above program produces the following output:
The given number is : 22 Is the number 22 is keith number: false