Write a Java Program to pass method call as arguments to another method

Write a Java Program to pass method call as arguments to another method

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Main {

  // calculate the sum
  public int add(int a, int b) {

    // calculate sum
    int sum = a + b;
    return sum;
  }

  // calculate the square
  public void square(int num) {
    int result = num * num;
    System.out.println(result);    // prints 576
  }
  public static void main(String[] args) {

    Main obj = new Main();

    // call the square() method
    // passing add() as an argument
    obj.square(obj.add(15, 9));

  }
}

Final output

Leave a Comment