Ask Question
11 June, 08:09

Write a method called printRange that accepts two integers as arguments and prints the sequence of numbers between the two arguments, separated by spaces. Print an increasing sequence if the first argument is smaller than the second; otherwise, print a decreasing sequence. If the two numbers are the same, that number should be printed by itself. Here are some sample calls to printRange:printRange (2, 7); printRange (19, 11); printRange (5, 5); The output produced from these calls should be the following sequences of numbers:2 3 4 5 6 719 18 17 16 15 14 13 12 115Test the method using the following main program:import java. util.*; / / for Scannerpublic class Lab4Q2 {public static void main (String[] args) {Scanner console = new Scanner (System. in); System. out. print ("Enter a positive integer: "); int num1 = console. nextInt (); System. out. print ("/nEnter a second positive integer: "); int num2 = console. nextInt (); System. out. println (); printRange (num1, num2); }

+1
Answers (1)
  1. 11 June, 08:57
    0
    import java. util.*;

    / / for Scanner

    public class Lab4Q2{

    public static void main (String[] args) {

    Scanner console = new Scanner (System. in);

    System. out. print ("Enter a positive integer: ");

    int num1 = console. nextInt ();

    System. out. print ("/nEnter a second positive integer: ");

    int num2 = console. nextInt ();

    System. out. println ();

    printRange (num1, num2);

    }

    public static void printRange (int a, int b) {

    if (a = = b) {

    System. out. print (a);

    } else if (a < b) {

    for (int i = a; i < = b; i++) {

    System. out. print (i + " ");

    }

    }

    else if (a > b) {

    for (int i = a; i > = b; i--) {

    System. out. print (i + " ");

    }

    }

    }

    }

    Explanation:

    In the printRange method that is called from the main method; we pass the two parameters of numbers entered as 'a' and 'b'. First, we check if 'a' and 'b' are the same, then we output a single instance of the input.

    Next, we check if the first input is less than the second input then we output in ascending order.

    Lastly, we check if the first input is greater than the second input then we output in descending order.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a method called printRange that accepts two integers as arguments and prints the sequence of numbers between the two arguments, ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers