81.3k views
0 votes
Implement a function named largerThanN that accepts three arguments: an array of type int, the size of the array of type int and an int number n. The function should display all of the numbers in the array that are greater than the number n. in the main() function create an array named numbers containing 10 integers : 30,20,50,2,-1,44,3,12,90,32 Ask the user to enter a number. Call the the function largerThanN and pass the array , the size and the number that user entered.

User Root
by
4.9k points

1 Answer

3 votes

Answer:

import java.util.Scanner;

public class NewArray {

//Main Method begins here

public static void main(String[] args) {

int [] numbers = {30,20,50,2,-1,44,3,12,90,32}; //The given array

Scanner in = new Scanner(System.in);

System.out.println("enter a number");

int n = in.nextInt(); //Receive the number from the user

//Calling the method largerThanN

largerThanN(numbers, numbers.length,n);

}

//The method largerThanN begins here

public static void largerThanN(int []intArray, int arraySize, int n){

for(int i = 0; i < intArray.length; i++){

if(intArray[i] > n){

System.out.println(intArray[i]);

}

}

}

}

Step-by-step explanation:

Uisng the Java prograamming language the solution is provided above.

Please see the comments within the code for detailed explanation

The key logic within the method largerThanN () is using a for loop to iterate the entire array. Within the for loop use an if statement to compare each element with the the number n entered by the user

User Naltatis
by
5.3k points