Question

In: Computer Science

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.

Solutions

Expert Solution

Java code:

import java.util.Scanner;
public class Main
{
   public static void main(String[] args){
       Scanner input=new Scanner(System.in);
       //initializing starting value,ending value,target,first and second half values,sum of them and the total of digits of sum
       int start,end,target,count=0,h1,h2,sum,total;
       //asking for starting and ending value
       System.out.print("Enter starting and ending value: ");
       //accepting starting value
       start=input.nextInt();
       //accepting ending value
       end=input.nextInt();
       //asking for target
       System.out.print("Enter target: ");
       //accepting target
       target=input.nextInt();
       //looping from start to end
       for(int i=start;i<=end;i++){
            //finding first half value
            h1=i/100;
            //finding second half value
            h2=i%100;
            //finding their sum
            sum=h1+h2;
            //initializing it's digits total as 0
            total=0;
            //looping till sum is 0
            while(sum!=0){
                //adding last digit of sum to total
                total+=sum%10;
                //removing last digit of sum
                sum/=10;
            }
            //checking if total equals to target
            if(total==target)
            //incrementing count
                count++;
       }
       //printing count
       System.out.println("Number of instances="+count);
   }
}


Screenshot:


Input and Output:


Related Solutions

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...
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle...
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle will have one private int that represents the countable value in the Bottle. Please use one of these names: cookies, marbles, M&Ms, pennies, nickels, dimes or pebbles. The class has these 14 methods: read()(please use a while loop to prompt for an acceptable value), set(int), set(Bottle), get(), (returns the value stored in Bottle), add(Bottle), subtract(Bottle), multiply(Bottle), divide(Bottle), add(int), subtract(int), multiply(int), divide(int), equals(Bottle), and toString()(toString()...
​​​​​​​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
JAVA CODE // Write a method called example that will do several things. // It has...
JAVA CODE // Write a method called example that will do several things. // It has two integer array parameters of unknown varying lengths. // It returns an integer array that contains each of the first array values // divided by two and each of the second array values added to the number 100. // It prints each value of the first array and then prints that value // divided by two on a separate line. It then prints each...
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove,...
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (After deleting an element, the number of elements in the array is reduced by 1.) Assume that...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to use all methods, also switch statements. Write a Temperature class. The class will have three conversion methods: toCelsius(), toKelvin() and toFahrenheit(). These methods will return a Temperature in those three scales equal to the this temperature. Note that the value of this is not changed in these conversions. In addition to these three conversion methods the class will have methods add(Temperature), subtract(Temperature), multiply(Temperature), and...
JAVA CODE FOR BEGINNERS!! DON'T USE FOR OR WHILE METHODS PLEASE! Write a program that reads...
JAVA CODE FOR BEGINNERS!! DON'T USE FOR OR WHILE METHODS PLEASE! Write a program that reads three strings from the keyboard. Although the strings are in no particular order, display the string that would be second if they were arranged lexicographically.
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
Write this code in java and don't forget to comment every step. Write a method which...
Write this code in java and don't forget to comment every step. Write a method which asks a baker how hot their water is, and prints out whether it is OK to make bread with the water. If the water is at or above 110F, your method should print "Too Warm." If the water is below 90.5F, print "Too Cold." If it is in between, print "Just right to bake!" For example, if the user inputs the number 100.5, the...
Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT