Question

In: Computer Science

write a java code. Consider a four digit number such as 6587. Split it at two...

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.

Solutions

Expert Solution

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 :


Related Solutions

write a java code, please do not use method and demo Consider a four digit number...
write a java code, please do not use method and demo 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.
Java Code!!!! A five-digit number is said to be friendly if: the leftmost digit is divisible...
Java Code!!!! A five-digit number is said to be friendly if: the leftmost digit is divisible by 1 and the leftmost two digits are divisible by 2 and the leftmost 3 digits are divisible by 3 and the leftmost 4 digits are divisible by 4 and leftmost 5 digits (the five-digit number itself) is divisible by 5. For example, the number 42325 is a friendly number: 4 is divisible by 1 and 42 is divisible by 2 and 423 is...
In Java: "The program should be able to read a two-digit number d from the standard...
In Java: "The program should be able to read a two-digit number d from the standard input using Scanner class and outputs the second digit followed by a string literal "<-->" followed by the first digit. Run your program and make sure it prints 2<-->5 when d=52"
​​​​​​​ASAP PLEASE Write a python code that prompts a user to input a 10-digit phone number....
​​​​​​​ASAP PLEASE Write a python code that prompts a user to input a 10-digit phone number. The output of your code is the phone number’s  area code, prefix and line number separated with a space on a single line e.g INPUT 2022745512 OUTPUT 202 274 5512 write a code that will prompt a user to input 5 integers and output its largest value. PYTHON e.g INPUT 2 4 6 1 3 OUTPUT Largest value is: 6
Suppose that a check digit is assigned to a four-digit number by appending the remainder after...
Suppose that a check digit is assigned to a four-digit number by appending the remainder after division by 7. If the number 36806 has a single-digit error in the first position, determine the possibilities for the correct number.
I need a java code Write a simple program to prompt the user for a number...
I need a java code Write a simple program to prompt the user for a number between 1 and 12 (inclusive) and print out the corresponding month. For example:   The 12th month is December. Use a java "switch" statement to convert from the number (1-12) to the month. Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12 inclusive and if not, prompt the user again until getting the correct...
Write a java code snippet to prompt the user for the number of names they’d like...
Write a java code snippet to prompt the user for the number of names they’d like to enter. Create a new array of the size chosen by the user and prompt the user for each of the names. Output the list of names in reverse order.
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of...
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of the following sorting methods (Insertion Sort, Selection Sort, Quick Sort, and Merge Sort) to sort ArrayList of objects using Comaprable interface. (60 points)
Write a java code that: 1) Takes as an argument an integer number, say N 2)...
Write a java code that: 1) Takes as an argument an integer number, say N 2) Creates an array of size N 3) Populates the array with random numbers between 0 and 10 * N. This is, the values of the elements of the array should be random numbers between 0 and 10 * N. 4) Finally, the code outputs the index of the smallest element and the index of the largest element in the array
Write Java code for each of the following problem a. Two players, a and b, try...
Write Java code for each of the following problem a. Two players, a and b, try to guess the cost of an item. Whoever gets closest to the price without going over is the winner. Return the value of the best bid. If both players guessed too high, return -1. example: closestGuess(97, 91, 100) → 97 closestGuess(3, 51, 50) → 3 closestGuess(12, 11, 10) → -1 b. Given a non-empty string, return true if at least half of the characters...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT