Question

In: Computer Science

Nice Number Programming: Nice program ask user to enter three integers from keyboard (java console), the...

Nice Number Programming:
Nice program ask user to enter three integers from keyboard (java console), the three integers
represent left bound, right bound and arbitrary digit ‘m’, where left bound is less than right bound.
Program should print all nice numbers in the given range that doesn’t contain digit 'm'. The number is
nice if its every digit is larger than the sum of digits which are on the right side of that digit. For example
741 is a nice number since 4> 1, and 7> 4+1. with digit m=2
Write a complete program in Java that Call only One method (niceNumbers method) that will print all nice
numbers excluding a given digit ‘m’ that also entered by user ?
Input Sample :
Enter Left bound: 740
Enter Right bound:850
Enter digit to exclude it:2
Output sample:
Nice Numbers in Range Left=740, Right=850 with exclude digit m= 2 are:
740 741 750 751 760 810 830 831 840 841 843 850
Your method prototype is like this:
public static void niceNumbers(int left, int right, int dight) {

Solutions

Expert Solution

Here is the Java code to the given question.

Detailed comments are added for better understanding.

Two sample outputs are added at the end.

import java.util.Scanner;

public class NiceNumber {

    public static void niceNumbers(int left, int right, int digit) {

        int sum;         /*used to store the sum of all the digits which are on the right side of present digit*/

        int currentDigit;      /*used to store the current digit being processed*/

        int currentNumber;      /*used to store the current number being processed*/

        System.out.println("Nice Numbers in Range Left="+left+", Right="+right+" with exclude digit m="+digit+" are:");

        for(int i=left;i<=right;i++){       /*runs a loop from left bound to right bound*/

            currentNumber=i;         /*store the value of current 'i' in the variable 'currentNumber'*/

            currentDigit=currentNumber%10;     /*extracts the last digit from currentNumber and stores it in variable 'currentDigit'*/

            currentNumber=currentNumber/10;     /*divides the currentNumber by 10*/

            sum=currentDigit;              /*makes sum equal to the last digit of the number*/

            if(currentDigit==digit){      /*checks if the currentDigit is equal to the digit passed to the method*/

                continue;      /*This number is not nice number.So, the current iteration is stopped and now program moves to check next number*/

            }

            if(currentNumber==0){   /*This means that the number in current iteration is a single digit number.So, it is a nice number*/

                    System.out.print(i+" ");  /*As 'i' contains the value of currentNumber at the start of this for loop iteration, the value of i is printed out*/

            }

            while(currentNumber>0){       /*runs the loop as long as currentNumber is greater than 0*/

                currentDigit=currentNumber%10;     /*gets the last digit of the currentNumber*/

                currentNumber=currentNumber/10;     /*divides currentNumber by 10*/

                if(currentDigit==digit){        /*checks if current digit is equal to the digit passed to the number*/

                    break;          /*This number is not a nice number.So, the while loop is terminated */

                }

                else if(currentDigit<=sum){     /*checks if current digit is less than or equal to sum of all the digits to the right of it*/

                    break;            /*This number is not a nice number.So, the while loop is terminated */

                }

                sum=sum+currentDigit;    /*The sum of all the digits is incremented by the currentDigit value*/

                if(currentNumber==0){     /*checks if all the digits of the currentNumber are checked , which means that the value of current number at the start of for loop is a nice number*/

                    System.out.print(i+" ");  /*As 'i' contains the value of currentNumber at the start of this for loop iteration, the value of i is printed out*/

                }

            }

        }
    }

    public static void main(String[] args){

        int left;      /*used to store left bound*/

        int right;     /*used to store right bound*/

        int digit;     /*used to store arbitrary digit*/

        Scanner scanner=new Scanner(System.in);          /*creates new Scanner object*/

        System.out.println("Enter the left bound :");

        left=scanner.nextInt();

        System.out.println("Enter the right bound :");

        right=scanner.nextInt();

        System.out.println("Enter the arbitrary digit :");

        digit=scanner.nextInt();

        niceNumbers(left,right,digit);     /*calls the niceNumbers() method by passing left,right and digit as arguments*/

    }
}

Sample Output-1:

Sample output-2:


Related Solutions

Write the following program in Java. Ask the user to enter the number of rows and...
Write the following program in Java. Ask the user to enter the number of rows and columns for a 2-D array. Create the array and fill it with random integers using (int)(Math.random() * 20) (or whatever number you choose). Then, provide the user with several menu choices. 1. See the entire array. 2. See a specific row. 3. See a specific column. 4. See a specific value. 5. Calculate the average of each row. 6. Calculate the total of each...
Write a java program that will ask the user to enter integers (use loops) until -100...
Write a java program that will ask the user to enter integers (use loops) until -100 is entered. The program will find and display the greatest, the smallest, the sum and the average of the entered numbers. also write a separate driver class which will be used to run and display the greatest, the smallest, the sum and the average of the entered numbers. Thanks!!
Write a MIPS program that will ask the user to enter two numbers at the console...
Write a MIPS program that will ask the user to enter two numbers at the console and pass the values to a function that does multiplication
Write a mips assembly language program to ask the user to enter two integers A and...
Write a mips assembly language program to ask the user to enter two integers A and B and then display the result of computing the expression: A + 2B - 5.
Write a Python program to: ask the user to enter two integers: int1 and int2. The...
Write a Python program to: ask the user to enter two integers: int1 and int2. The program uses the exponential operator to calculate and then print the result when int1 is raised to the int2 power. You also want to calculate the result when int1 is raised to the .5 power; however, you realize that it is not possible to take the square root of a negative number. If the value for int1 that is entered is a negative number,...
write a program i java that ask the user to enter salary, user ID and username...
write a program i java that ask the user to enter salary, user ID and username and out put them
Write a program in Java to: Ask the user how many scores they want to enter...
Write a program in Java to: Ask the user how many scores they want to enter (=> Create an array based the entered size) Ask the user to enter their scores in the quarter (Fill the array with the entered scores(assigned to elements)) ==>> Use a "for" loop Find the greatest score Calculate and show the average Show the final grade based on the average (For example A,B,C ... ) Print the scores (the elements of the array) => Use...
Using Java! Write a program that ask prompt the user to enter a dollar amount as...
Using Java! Write a program that ask prompt the user to enter a dollar amount as double. Then, calculate how many quarters, dimes, nickels and pennies are in the dollar amount. For example: $2.56 = 10 quarters, 1 dime, 1 nickel and 1 cent. Print all of the values. Hint: Use Modulus operator and integer division when necessary.
JAVA Programming (Convert decimals to fractions) Write a program that prompts the user to enter a...
JAVA Programming (Convert decimals to fractions) Write a program that prompts the user to enter a decimal number and displays the number in a fraction. Hint: read the decimal number as a string, extract the integer part and fractional part from the string, and use the Rational class in LiveExample 13.13 to obtain a rational number for the decimal number. Use the template at https://liveexample.pearsoncmg.com/test/Exercise13_19.txt for your code. Sample Run 1 Enter a decimal number: 3.25 The fraction number is...
4. Prompt user to enter a double number from console. Round this number so it will...
4. Prompt user to enter a double number from console. Round this number so it will keep 2 places after the decimal point. Display new number. For example, number 12.3579 will round to 12.36; number 12.4321 will round to 12.43 This is meant to be written in Java 14.0
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT