keith number

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
https://java4school.com/important-number-programs-icse-part-4

Write a program to accept a number and print whether it is a keith number or not.

FOR EXAMPLE: 746, 404, 219 etc 
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

LEAVE A REPLY

Please enter your comment!
Please enter your name here