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  ...

Conditional Statements | if, If-else, "if" and "if else", Switch Break | Complete Java Placement Course | Lecture 3

                                    Java - Introduction to Programming Lecture 3 Conditional Statements Conditional statements are a fundamental concept in programming. They are used to make decisions based on certain conditions, allowing the program to perform different actions depending on the input or data it receives. In this article, we'll take a closer look at conditional statements in programming and how they can be used effectively. What are conditional statements? Conditional statements, also known as "if-then" statements, are used in programming to execute certain code if a condition is met. In other words, the program will perform a particular action if a particular condition is true. For example, a program may check if a user has ent...