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
Java programming Write the max-heapify code and test it and then write the program to find...
Java programming Write the max-heapify code and test it and then write the program to find the three largest values of the array without sorting the entire array to get values.
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 a program to find the prime numbers IN JAVA Ask user to input the integer...
Write a program to find the prime numbers IN JAVA Ask user to input the integer number test the number whether it is a prime number or not Then, print “true” or “false” depending on whether the number is prime or isn’t. Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. Use idea of user input, cumulative sum, and loop to solve...
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.
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?
Complete the following Java code. Compute the average of all the numbers in nums and store...
Complete the following Java code. Compute the average of all the numbers in nums and store it in a variable called average      public static void main(String[] args){                         int[] nums = <some array values>;
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 ≤...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT