Question

In: Computer Science

Write a java code to find the following. For numbers 501 to 999, how many numbers...

Write a java code to find the following. For numbers 501 to 999, how many numbers will have the sum of the digits equal to 10.

501, sum of digits=6
502, sum of digits=7
503, sum of digits=8
504, sum of digits=9
505, sum of digits=10
506, sum of digits=11

Solutions

Expert Solution

Here i am writing the code in the java. after this // i will explain about that line

First what is the logic for this program;

logic will be we will make a function or method in java which gives us sum of digit then after getting sum of digit we will compare and if match then we will count it.

code : ( Note: make sure your java program file name and in the code name should be same )

public class Main

{

    public static void main(String[] args) {     //program starts now

       int count=0;               // declaring the variable count which will count the sum of digit is equal to 10

       int temp=0;           // temp variable which will store sum of digit

       for(int i=501;i<=999;i++)      // for loop declaration 501 to 999

       {

           temp = sumFunction(i);   // calling sum function which is created in the below and store the sumfunction value in temp variable

           if(temp==10)             //now checking for equal to 10 if match then it will increment the value of count by 1

           {

               count++;

           }

       }

       System.out.print(count);      // after all done print the value of count then we get how many number are there in the range which sum of digit is equal to 10

    }

public static int sumFunction(int x)      // creating function and taking x variable

{

    int sum=0;           // declaring sum variable where sum will store

    while(x!=0)         // till x not equal to 0 the loop will run

    {

        sum=sum+x%10; // sum of digit algorithm,   

        x=x/10;     

    }

    return sum;         // when loop complete the function will return sum.

}

}

Output is 20


Related Solutions

i need code in javascript or htmlt convert 0 to 999 numbers into word
i need code in javascript or htmlt convert 0 to 999 numbers into word
Input 100 numbers and find and output how many numbers are positive and negative, find and...
Input 100 numbers and find and output how many numbers are positive and negative, find and output average of these numbers Write using vb.net
Write Java code that allows a user to repeatedly enter numbers. Each time the user enters...
Write Java code that allows a user to repeatedly enter numbers. Each time the user enters a number, the program should print out the average of the last 3 numbers (or all numbers if 3 or less have been entered). I would like a detailed explanation so that a beginner level Java programmer could understand.
java please 1. Write a Java program to generate random numbers in the following range a....
java please 1. Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 2. Write statements that assign random integers to the variable n in the following ranges: a) 1 ≤ n ≤ 2 b) 1 ≤ n ≤ 100 c) 0 ≤ n ≤ 9 d) 1000 ≤...
For a 3 digit code with distinct numbers. (0-9) How many combinations to get the code...
For a 3 digit code with distinct numbers. (0-9) How many combinations to get the code (max)? How many combinations if you remember the middle number is 1?
Write a java code that first discards as many whitespace characters as necessary until the first...
Write a java code that first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace...
write a code using c++. Find the first 10 prime numbers and store them in an...
write a code using c++. Find the first 10 prime numbers and store them in an array.
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
JAVA Problem 1: [15 marks] Find the average of an array of numbers (filename: FindAverage.java) Write...
JAVA Problem 1: [15 marks] Find the average of an array of numbers (filename: FindAverage.java) Write two overloaded methods with the following headers to find the average of an array of integer values and an array of double values: public static double average(int[] num) public static double average(double[] num) In the main method, first create an array of integer values with the array size of 5 and invoke the first method to get and display the average of the first...
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are...
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are that it returns an array of all the elements of the int[] array numbers which are coprime with x } Note that the array that is returned may be an empty array--you will have to count how many times gcf(x, numbers[i]) == 1. ASSUME numbers is not null and not empty.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT