Question
Check whether a numberĀ is Keith number.
A number is called Keith number if it appears in a special sequence (defined below) generated using its digits. The special sequence has first n terms as digits of number and other terms are recursively evaluated as sum of previous n terms
ENTER A NUMBER:
197
197 is a keith number
197 has 3 digits, so n = 3
The number is Keith because it 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, .....
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class keithNumber
{
public static void main()
{
int digits[];
int num=0,count=0,i=0,temp=0,sum=0;
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE NUMBER");
num=sc.nextInt();
temp=num;
while(temp>0)
{
count++;
temp=temp/10;
}
digits=new int[count];
System.out.println(count);
temp=num;
while(temp>0)
{
digits[count-1]=temp%10;
temp/=10;
count--;
}
while(sum< num)
{
sum=0;
for(i=0;i< digits.length;i++)
{
sum=sum+digits[i];
}
for(i=0;i< digits.length-1;i++)
{
digits[i]=digits[i+1];
}
digits[digits.length-1]=sum;
}
if(sum==num)
{
System.out.println(num+" is keith number");
}
else
{
System.out.println(num+" is not a keith number");
}
}
}