Java Program to Check Keith Number

In the previous article, we have discussed Java Program to Check Fascinating Number

In this article we are going to understand what Keith number is and how we can check whether a number is Keith number or not in Java with examples.

Program to Check Keith Number

Keith numbers are numbers when it is arranged in a special number sequence at some point we will get the original number.

How we get that sequence ?

  1. First find the number of digits (say it is n) in the actual number (say it is x).
  2. Then add each digits of the number, you will get a new number (say result).
  3. Then add last ‘n’ numbers(from found digits) including the result.
  4. Keep on doing from step-2 repeatedly at some point you will get a number which is same as the actual/original number or you will cross the original number.
  5. If you found a number in sequence which is same as original number then it is Keith number.
For example: 

19 (original number, total 2 digits)
1,9 (digit separated)
1+9 =10 (2 digits added)
9+10=19 (2 digits added along with previous result i.e 10, now got original number)
So, 19 is Keith number.

Example :

  • 742: Keith number
  • 1104: Keith number
  • 19: Keith number
  • 1537: Keith number

In the above examples the numbers 742 and 19 are Keith numbers.

Let’s see different ways to check Keith number.

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Approach :

  1. We ask the user to enter a number and store it.
  2. We separate each digit of the number and add it to find the next term in the sequence. This step is repeated until we get the number back.
  3. If the numbered is generated then it is said to be a Keith number.

Method-1: Java Program to Check Keith Number By User Input Value

import java.util.*;
public class KeithNumber
{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();
 
        // List to store the terms
        ArrayList<Integer> terms = new ArrayList<Integer>();
        int temp = num, count = 0;

        while(temp>0)
        {
            terms.add(temp%10);
            temp /= 10;
            count++;
        }

        // Reversing the list
        Collections.reverse(terms);
        int next = 0, iter = count;

        // Next term for the series
        while(next<num)
        {
            next = 0;
            for(int a = 1; a <= count; a++)
                next = next + terms.get(iter-a);
            terms.add(next);
            iter++;

        }
        // Returns whether the number matches the generated number
        if(next==num)
            System.out.println(num+" is a Keith Number");
        else
            System.out.println(num+" is Not a Keith Number");
   }
}
Output:
Case-1

Enter a number : 19
19 is a Keith Number

Case-2

Enter a number : 18
18 is Not a Keith Number

Method-2: Java Program to Check Keith Number By User Defined Method

import java.util.*;
public class KeithNumber{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();

        if(keithCheck(num))
        {
            System.out.println(num+" is a Keith Number");
        }
        else
        {
            System.out.println(num+" is Not a Keith Number");
        }
    }

    // method to check for Keith Number
    static boolean keithCheck(int num)
    {
        // List to store the terms
        ArrayList<Integer> terms = new ArrayList<Integer>();
        int temp = num, count = 0;

        while(temp>0)
        {
            terms.add(temp%10);
            temp /= 10;
            count++;
        }

        // Reversing the list
        Collections.reverse(terms);
        int next = 0, iter = count;

        // Next term for the series
        while(next<num)
        {
            next = 0;
            for(int a = 1; a <= count; a++)
                next = next + terms.get(iter-a);
            terms.add(next);
            iter++;

        }
        // Returns whether the number matches the generated number
        return(next==num);
    }
}
Output: 

Case-1 

Enter a number : 19 
19 is a Keith Number 

Case-2 

Enter a number : 18 
18 is Not a Keith Number

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Related Java Programs: