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
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.
The following Java code is set up to ask how many people are attending a meeting...
The following Java code is set up to ask how many people are attending a meeting and checks these user generate responses with replies, using the do while setup. To end the loop you type 0, change this to accept the answer "Y" to continue the loop after every response and "N" to end the loop with every case type. (Y,y,N,n) Meeting.java ------ import java.util.Scanner; public class Meeting {    public static void main(String[] args) {        Scanner input...
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 a program in java which store 10 numbers and find the sum of odd and...
Write a program in java which store 10 numbers and find the sum of odd and even numbers. Create a program that uses a two dimensional array that can store integer values inside. (Just create an array with your own defined rows and columns). Make a method called Square, which gets each of the value inside the array and squares it. Make another method called ShowNumbers which shows the squared numbers. Write a program in java which has an array...
Write a program in java which store 10 numbers and find the sum of odd and...
Write a program in java which store 10 numbers and find the sum of odd and even numbers. Create a program that uses a two dimensional array that can store integer values inside. (Just create an array with your own defined rows and columns). Make a method called Square, which gets each of the value inside the array and squares it. Make another method called ShowNumbers which shows the squared numbers.
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?
a/  write  4 numbers from your choice b/ Write a java code to create a linked list containing...
a/  write  4 numbers from your choice b/ Write a java code to create a linked list containing the 4 numbers
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT