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

package learning;
import java.util.*;
class Main {
        
        public static void niceNumbers(int left,int right,int digit) {
                // Traversing the range
                for(int i=left ; i<= right ; ++i) {
                        // Flag to check correctness
                        boolean flag = true;
                        int sum=0,temp = i;
                        // Test loop where the value will be tested for for nice number
                        // If valuse fails any test then flag will become false and loop will break
                        while(temp!=0) {
                                int r = temp % 10;
                                if(r==digit) {
                                        flag = false;
                                        break;
                                }
                                if(r > sum || temp==i) {
                                        sum=sum+r;
                                        temp = temp/10;
                                }else {
                                        flag = false;
                                        break;
                                }       
                        }
                        // If flag passes all tests then print i
                        if(flag) {
                                System.out.print(i + " ");
                        }
                }
                return;
        }
        
        public static void main(String[] args) {
                
                Scanner input = new Scanner(System.in);
                System.out.print("Enter Left Bound: ");
                int left = input.nextInt();
                System.out.print("Enter Right Bound: ");
                int right = input.nextInt();
                System.out.print("Enter digit to exclude it: ");
                int digit = input.nextInt();
                
                // Calling method 
                niceNumbers(left,right,digit);
        }
}

OUTPUT:

Kindly go through the comments for better understanding of the code.


Related Solutions

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...
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...
JAVA PROGRAMMING Write a program that ask the user for a number then prints the cube...
JAVA PROGRAMMING Write a program that ask the user for a number then prints the cube of the number. The program should be called CubeNumbers. Use a value returning method with parameter, call the method myCube.
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 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 compute xy
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,...
JAVA Write a test program that prompts the user to enter a series of integers and...
JAVA Write a test program that prompts the user to enter a series of integers and displays whether the series contains runLength consecutive same-valued elements. Your program’s main() must prompt the user to enter the input size - i.e., the number of values in the series, the number of consecutive same-valued elements to match, and the sequence of integer values to check. The return value indicates whether at least one run of runLength elements exists in values. Implement the test...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT