Question

* Please write the answer without the explanation. * Please star with Public class. Write a...

* Please write the answer without the explanation.

* Please star with Public class.

Write a program that asks the user to enter two positive integers (input validation), then the program should pass the numbers to the method to display all even numbers between those two integers inclusive in the following format.

If enter 11 and 18, the output should be: (12, 14, 16, 18)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Explanation::

  • Code in JAVA is given below
  • Please read comments for better understanding of the code
  • Screenshots of the OUTPUT are given at the end of the code


Code in JAVA::

import java.util.Scanner;

public class EvenNumbers {

   public static void main(String[] args)throws Exception {
       /*
       * We are using Scanner class to take input from the user
       * **/
       Scanner sc=new Scanner(System.in);
      
       /**
       * Two integer variables are declared to store lower and higher value
       * */
       int lower,higher;
      
       /**
       * Prompting user to enter positive values
       * If user enters negative value for lower then
       * We ask again to enter the value of lower
       * */
       while(true) {
           System.out.print("Enter value of lower for the range : ");
           lower = sc.nextInt();
           if(lower>=0) {
               break;
           }
           System.out.println("Please enter positive value for lower.");
       }
       /**
       * Prompting user to enter value that is greater then lower
       * */
       while(true) {
           System.out.print("Enter value of higher for the range : ");
           higher = sc.nextInt();
           if(higher>lower) {
               break;
           }
           System.out.println("Please enter value greater then lower.");
       }
      
       /**
       * Now that we have both valid inputs let's call the function to
       * print only positive value
       * */
       System.out.println(" The outpur is:");
       displayEven(lower,higher);

   }

   private static void displayEven(int lower, int higher) {
       /**
       * First we print the open bracket (
       * */
       System.out.print("(");
      
       /**
       * Then we run the for loop that start from
       * lower to hogher both inclusive
       * */
       for(int i=lower;i<=higher;i++) {
           /**
           * To check if current value of i is even
           * we must see if remainder is 0 when i is divided by 2
           * */
           if(i%2==0) {
               /**
               * Here in if we check if current i is equal to higher
               * or higher-1, if so then which means we are at last number.
               * So we must print the number and closing bracket )
               * */
               if(higher==i || higher-1==i) {
                   System.out.print(i+")");
               }else {
                   /**
                   * Else we just print the number along with comma
                   * */
                   System.out.print(i+", ");
               }
           }
       }/*For loop ends here*/
       System.out.println();
      
   }/*displayEven function ends here*/
  
}/*class ends here*/

OUTPUT::

TEST CASE 1:

TEST CASE 2:

Please provide the feedback!!

Thank You!!

Add a comment
Know the answer?
Add Answer to:
* Please write the answer without the explanation. * Please star with Public class. Write a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that asks the user how many integers they would like to enter. You...

    Write a program that asks the user how many integers they would like to enter. You can assume they will enter an integer >= 1. The program will then prompt the user to enter that many integers. After all the numbers have been entered, the program should display the largest and smallest of those numbers (no, you cannot use lists, or any other material we haven't covered). When you run your program it should match the following format: How many...

  • Please help and please satisfy the directions. The directions are under program 3. port JavaScore. public...

    Please help and please satisfy the directions. The directions are under program 3. port JavaScore. public class Example 4 [ public static int getint String strum - JOptionPane.showinput Dialog("Enter an integer"); return integer.parseInt(strum): public static void main(String args Scanner input = new Scanner (System.in): System.out.println("Enter 2 integers"); intx input.nextInt(); inty input.nextInt(): 1/ these are simple is if b ) System.out.println("The first number is positive): if (0 y0) System.out.println("Both numbers are positive"); ( 11 y) System.out.println("At least one number is positive");...

  • Hi please help me with this zybooks question asap. Thanks 4.17 Chapter 4 Program: Sum of...

    Hi please help me with this zybooks question asap. Thanks 4.17 Chapter 4 Program: Sum of Numbers/C++ program Write a C ++ program that asks the user for a positive integer value by prompting "Enter a positive integer number: ", read in that number, then use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2,3, 4…50....

  • Can you please write simple program with comments. A Palindrome is a string that is spelled...

    Can you please write simple program with comments. A Palindrome is a string that is spelled the same way forward and backward (example: radar). Write a Java program that asks the user to input a string and tests whether the string is a Palindrome or not. Display the message: "The string is a Palindrome" if it is, or "The string is NOT a Palindrome" if it is not. Assume that the user will enter a string without any spaces. The...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • PLEASE HELP!!!I need help with the following JAVA PROGRAMS. Please ready carefully. So this comes from...

    PLEASE HELP!!!I need help with the following JAVA PROGRAMS. Please ready carefully. So this comes from tony gaddis starting out with java book. I have included the screenshot of the problems for reference. I need HELP implementing these two problems TOGETHER. There should be TWO class files. One which has TWO methods (one to implement problem 8, and one to implement problem 9). The second class should consist of the MAIN program with the MAIN method which calls those methods....

  • Please answer this question in C++. Also, please write it with 'using namespace std' because the other ways some of you do it is confusing. Class Exercise Write a program that does the follow...

    Please answer this question in C++. Also, please write it with 'using namespace std' because the other ways some of you do it is confusing. Class Exercise Write a program that does the following: 1. Defines a class named Data that has the following members a. A vector of integers entitled numbers (Private) b. Methods (Functions) (Public): i. Display_Menu Prompts the user to choose one of the following 0. Quit 1. Input Numbers 2. Display Numbers 3. Search Numbers 4....

  • c++ class, coding using code blocker MinilabLoopLogic The program: You are to write a program called...

    c++ class, coding using code blocker MinilabLoopLogic The program: You are to write a program called MinilabLoopLogic which does the following Asks the user to enter 2 integers (can be one prompt or two) Gets and stores the ints Explains that the program will generate all integers that are between the numbers and are divisible by a third integer . e Asks the user for the number they should be divisible by Gets and stores that int Since 0 will...

  • Write a c++ complete program to meet the specifications. The program should prompt the user for...

    Write a c++ complete program to meet the specifications. The program should prompt the user for a positive integer. The program should print a message whether the integer is even or odd. The looping should end when the user enters a negative number. The negative number will not be tested for even or odd. The program will print out a message of how many numbers were entered (not counting the negative number) and how many even and odd numbers were...

  • C++ coding answer 5. Write a program that asks the user for a positive integer value....

    C++ coding answer 5. Write a program that asks the user for a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, ... 50. Input Validation: Do not accept a negative starting number.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT