In: Computer Science
write a java code.
Consider a four digit number such as 6587. Split it at two digits, as 65 and 87. Sum of 65 and 87 is 152. Sum of the digits of 152 is 8. Given the starting and ending four digit numbers and a given target number such as 10, write a program to compute and print the number of instances where the sum of digits equals the target.
import java.util.*;
public class Main
{
    public static boolean checkNumber(int number, int target)
    {
        //to store numbers after spliting 4 digit number
        int num1, num2;
        
        //num1
        num1 = number/100; 
        
        //num2
            num2 = number%100; 
            
            //sum of both spilted numbers 
            int sumOfTwoNos = num1 + num2;
            
            //two store sum of the sum of spilted numbers
            int sum = 0,n;
            
            //to find sum
            while(sumOfTwoNos > 0)
        {
            n = sumOfTwoNos % 10;
            sum = sum + n;
            sumOfTwoNos = sumOfTwoNos / 10;
        }
        
        //if sum is same as target result will be true, else false
        if(sum == target)
            return true;
        else
            return false;
    }
    
        public static void main(String[] args) {
            
            Scanner sc = new Scanner(System.in);
            
            //to store start value, end value and target
            int start, end, target;
            
            //to store if the number can fulfill our requirement means sum of that number must be target
            boolean result;
            
            //user input for starting value
                System.out.println("Enter starting number(4 digit) : ");
                start = sc.nextInt();
                
                //user input for ending value in which end must be greater than start value
                do{
                    System.out.println("Enter ending number(4 digit) : ");
                    end = sc.nextInt();
                    //to check end is greater than start or not
                    if(end < start)
                        System.out.println("Ending must be greater than starting");
                }while(end < start);
                
                //user input for target value
                System.out.println("Enter target : ");
                target = sc.nextInt();
                
                //to calculate a number is fulfill our requirement and if yes then print
                //loop will executes from starting value to ending value, included
                System.out.println("Required Numbers : ");
                for(int i = start; i <= end; i++)
                {
                    //function to check given requirement if it satiesfies then result will be true, else false
                    result = checkNumber(i, target);
                    
                    //if result true then it will that number
                    if(result)
                    {
                        System.out.println(i);
                    }
                }
                
        }
}
Please refer code screenshot for your understanding of indentations


Output sample :
