In this tutorial you will learn about the Program to check Keith Number in Java and its application with practical example.
In this tutorial, we will learn to create a Java Program to check Keith Numbers in Java using Java programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
- If-else statements in Java.
- For loop in Java.
What is Keith number in mathematics?
In mathematics a Keith numberKeith numbers are also called repfigit and it is a positive N-digit integer value where Number is greater than 9 such that if we create a series like – Fibonacci( Where each numbers/term in the following series is the sum of the n previous numbers/terms) and this series is formed by the sum of first n terms taken as the integervalue of the number N, then N itself occurs as a term in the series. i.e. 197 is a Keith number as it makes the series asfollows:
1, 9, 7,
1+9+7=17,
9+7+17=33,
7+17+33=57,
17+33+57=107,
33+57+107=197 ->(Keith).
Keith numbers are known as repfigit (repetitive fibonacci-like digit) numbers.
Here: –
N = number of digits of the term.
n = number of terms in the series.
Java Program to Find Keith Number
In this program we will find the number given by the user is a Keith number or not with the help of Java Program. We will first declare and initializethe required variables for the program. Next, we will prompt the user to input the value in integer format. Later we will check the given value is a Keith number or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import java.util.*; class KeithNumber { Public static void main () { Scanner sc = new Scanner(System.in); System.out.println("Enter any number to check"); int no = sc.nextInt(); // taking number value form user.. int no1 = no; String s = Integer.toString(no); int d = s.length(); int arr[] =new int[no]; int i , sum; for(i=d-1;i>=0;i--) { arr[i] = no1%10; no1=no1/10; } i = d , sum =0; while (sum <no) { sum =0 for(int j=1, j<=d,j++) { sum = sum+arr[i-j]; } arr[i]=sum; i++; if (sum ==no) System.out.println("The given number is a keith number"); else System.out.println("The given number is not a Keith Number"); } } |
Output
In the above program, we have first declared and initialized a set of variables required in the program.
- no = it will hold entered length.
- no1= it will replicated value of no1 for digit calculation.
- d= it will hold the number of digits.
- sum = it will hold the summation of no. of term terms.
- arr[]= is a array for holding all the terms of series.
- i,j = will hold the numerical value to execute the loop.
Keith number is a term/entity which is repeated on a series when of a number is break down into the respective digits and add to get the desired number again.
After that take valuesin to an array.
Calculating the Keith Number from array we made.