Skip to main content

Write a function to multiply 2 numbers | write a program to multiply two numbers in java

  Write a function to multiply 2  numbers   


Java is a popular programming language that's widely used in developing a range of applications. In this article, we'll explore how to write a function to multiply two numbers in Java.

To begin with, a function is a self-contained code unit that performs a specific task. In Java, we use the "public" keyword to indicate that a function is available to other parts of the program. For instance, to write a function that multiplies two numbers, we would start by declaring the function as follows:

Java
public static int multiply(int num1, int num2) {
int result = num1 * num2;
return result;
 }

The above function takes two integer arguments, "num1" and "num2," and returns their product. The "static" keyword means that the function belongs to the class, not to an instance of the class.

To use this function in your program, you need to call it from another part of your code. Here's an example of how to call the "multiply" function:

Java
int a = 5
int b = 10
int result = multiply(a, b);
 System.out.println(result);


In the above example, we declare two integer variables "a" and "b" and assign them values of 5 and 10, respectively. We then call the "multiply" function, passing in the two variables, and assign the result to a new variable called "result." Finally, we print the value of "result" to the console.

Writing a function to multiply two numbers in Java is a simple task that can make your code more efficient and reusable. By encapsulating the multiplication operation in a function, you can easily call it from different parts of your program, saving you time and reducing the chances of introducing errors.

In conclusion, the "multiply" function we've shown is just one example of how you can write functions to perform specific tasks in Java. Functions can simplify your code and make it more readable and maintainable. So, go ahead and try writing your own functions to perform different operations in your Java programs.

Here the long and simple java program to function to multiply 2 numbers.

import java.util.*;

 

public class Functions {

 

   //Multiply 2 numbers

   public static int multiply(int a, int b) {

       return a*b;

   }

   public static void main(String args[]) {

       Scanner sc = new Scanner(System.in);

       int a = sc.nextInt();

       int b = sc.nextInt();

 

       int result = multiply(a, b);

       System.out.println(result);

   }

}


 READ THIS ALSO:- 


  1. Road map to learn the java | java developer roadmap 2023 | advanced java roadmap.
  1. Bit Manipulation |Get Bit | Set Bit | Update Bit | Lecture 14
  1. 2D Arrays | Java Complete Placement Course | Lecture 11
  1. Variables & Data Types (Lecture 2) | Primitive Data Types| Non-Primitive Data types
  1. Recursion Class 2 | Recursion in One Shot | 9 Best Problems
  1. Time & Space Complexity | Time & Space Complexity in java and DSA | (Lecture 8 )
  1. Sorting in Java | Bubble Sort Algorithm | Java Placement Course 16
  1. String Builder | Reverse a String (using StringBuilder class)| Get A Character from Index Lecture 13 
  1. Top Beginner Patterns in Java: How to Print Star, Number and Character (Lecture 5)
  1. Functions in Java | Practice Questions | Complete Placement Course | Lecture 7


 My Product's:- 





4) Earbud

6) Tripod











Comments

Popular posts from this blog

Enter 3 numbers from the user & make a function to print their average in java, c++, python, java.

Enter 3 numbers from the user & make a function to print their average. To find the average of three numbers, you need to add the three numbers together and then divide the sum by three. Here's how you can write a function in C++, C, Python, and Java to take three numbers from the user and calculate their average: JAVA import java . util .*; public class Solutions {     public static void main ( String args []) {        Scanner sc = new Scanner ( System . in );        int a = sc . nextInt ();        int b = sc . nextInt ();        int c = sc . nextInt ();          int average = ( a + b + c ) / 3 ;        System . out . println ( average );    }    }   C++: // c++ #include <iostream> using namespace std ;...

2D Arrays | Java Complete Placement Course | Lecture 11

Java - Introduction to Programming Lecture 11 2D Arrays In Java It is similar to 2D matrices that we studied in 11th and 12th class. Creating a 2D Array - with new keyword int [][] marks = new int [ 3 ][ 3 ] ; Taking a matrix as an input and printing its elements. import java . util .*;   public class TwoDArrays {     public static void main ( String args []) {         Scanner sc = new Scanner ( System . in );         int rows = sc . nextInt ();         int cols = sc . nextInt ();           int [][] numbers = new int [ rows ][ cols ];           //input         //rows         for ( int i = 0 ; i < rows ; i ++) {             //columns  ...

Merge Sort | How do I learn merge sort?

       Merge Sort | For Beginners | Java Placement Course >> Merge sort is a popular sorting algorithm that utilizes a divide-and-conquer approach to sort elements in an array. The basic idea behind merge sort is to divide an unsorted array into two halves, recursively sort each half, and then merge the two sorted halves into a single sorted array. >>  Merge sort algorithm works by recursively dividing an array into two halves, sorting each half, and then merging them to obtain the sorted array. The basic steps of the merge sort algorithm are as follows: Divide the unsorted array into two halves, mid and left. Sort the left half recursively using merge sort algorithm. Sort the right half recursively using merge sort algorithm. Merge the two sorted halves to obtain the final sorted array. JAVA Implementation: // Merge sort public class MergeSort {     //time complexity=nlogn     public static void conquer ( int arr [], int ...