77.7k views
0 votes
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.

User AGO
by
5.0k points

1 Answer

4 votes

Answer:

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<arr.length; 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();

}

}

Step-by-step 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

User Luiss
by
5.5k points