Ask Question

In a program, write a method that accepts two arguments: an array of integers and a number n. The method should print all of the numbers in the array that are greater than the number n (in the order that they appear in the array, each on their own line). In the same file, create a main method and call your function using the following data sets: The array {1, 5, 10, 2, 4, - 3, 6} and the number 3. The array {10, 12, 15, 24} and the number 12.

+1
Answers (1)
  1. 8 July, 19:38
    0
    Here is code in java.

    //import package

    import java. util.*;

    import java. lang.*;

    import java. io.*;

    class Main

    {

    public static void main (String[] args) throws java. lang. Exception

    {

    try{

    / / declare and initialize array

    int[] arr1 = {1,5,10,2,4,-3,6};

    int num1=3;

    System. out. println ("Numbers greater than "+num1+" in the 1st array are : ");

    / / call the function to print the Number greater than 3

    fun (arr1, num1);

    / / declare and initialize array

    int[] arr2 = {10, 12, 15, 24};

    int num2 = 12;

    System. out. println ("Numbers greater than "+num2+" in the 2nd array are : ");

    / / call the function to print the Number greater than 12

    fun (arr2, num2);

    }catch (Exception ex) {

    return; }

    }

    public static void fun (int[] arr, int n)

    {

    for (int i=0; i
    {

    / / if the array element is greater than the value of n, then print it

    if (arr[i]>n)

    System. out. print (arr[i]+" ");

    }

    System. out. println ();

    }

    }

    Explanation:

    Declare and initialize an array "arr1" and a variable "num1" with 3. then call the function "fun () " with parameter "arr1" and a Number "num1", if the element of the array is greater that the value of "n" then it will print that element of the array. Similarly call the function "fun () " with parameter "arr2" and number "num2".

    Output:

    Numbers greater than 3 in the 1st array are:

    5 10 4 6

    Numbers greater than 12 in the 2nd array are:

    15 24
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “In a program, write a method that accepts two arguments: an array of integers and a number n. The method should print all of the numbers in ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers