Array-promo

Question

Program to print keith numbers between lower and upper range.
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 the number and other terms are recursively evaluated as sum of previous n terms

				
					ENTER THE Lower Range
25
ENTER THE Upper Range
300
31 50 53 57 89 117 122 125 197 210 237 278

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 keithNumberInGivenRange
{
    public static void main()
    {
        int digits[];
        int lowerRange=0,upperRange=0,count=0,i=0,temp=0,sum=0,j=0;
        Scanner sc=new Scanner(System.in);
        System.out.println("ENTER THE Lower Range");   
        lowerRange=sc.nextInt();
        System.out.println("ENTER THE Upper Range");   
        upperRange=sc.nextInt();
        for(i=lowerRange;i<=upperRange;i++)
        {
            
            temp=i;
            while(temp>0)
            {
                count++;
                temp=temp/10;
            }
            digits=new int[count];

            temp=i;
            while(temp>0)
            {
                digits[count-1]=temp%10;
                temp/=10;
                count--;
            }

            while(sum< i)
            {
                sum=0;
                for(j=0;j< digits.length;j++)
                {
                    sum=sum+digits[j];

                }
                for(j=0;j< digits.length-1;j++)
                {
                    digits[j]=digits[j+1];

                }
                digits[digits.length-1]=sum;

            }

            if(sum==i)
            {
                System.out.print(i+" ");   
            }

        }

    }
}

				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *