Calculations

Java program to multiply two numbers using method

Java program to multiply two numbers using the method

In this program, we will discuss the Java program to multiply two numbers using the method

In this topic, we will learn a simple concept of how to multiply two number in Java programming language using the Java method.

already we knew the same concept using the Java operator.

if you want to know about that, click here Java program to multiply two numbers

Program 1

the following programs have the following 4 different steps to completion

  • Get input from the user for num1,num2- Using Scanner class
  • Declare the variable to store the value
  • create the method with the return value
  • Calling the method

Java program to multiply two integers using the method

import java.util.Scanner;
public class ProductOfNumbur1{

public static void main(String args[]){

Scanner sc=new Scanner(System.in);
// create scanner object

System.out.print("Enter the first number: ");

int num1=sc.nextInt();
//this method reads value for num1 providing by user

System.out.print("Enter the second number: ");

int num2=sc.nextInt();
//this method reads value for num2 providing by user

sc.close();//closing the scanner class

calcProduct(num1,num2); //calling the method

}
//calcTotal method
public static void calcProduct(int x,int y){
    int result=0;
result=x*y;
System.out.println("product of two numbers  "+result);


}
}

When the above code is compiled and executed, it produces the following results

Enter the first number: 10 
Enter the second number: 33 
product of two numbers 330

In this program, the user is asked to enter two numbers(integer numbers). then it calculating product of those numbers using Java method and display on the screen.

Here, when the user enters the numbers, the first number stored in variable num1 and the second number stored the variable num2. Then, the product of the numbers is stored in the variable product.

Program 2

Java program to multiply two integers using the method with return

This is a program to calculate product of floating point values using Java method

import java.util.Scanner;
public class ProductOfNumbur{

public static void main(String args[]){

Scanner sc=new Scanner(System.in);
// create scanner object

System.out.print("Enter the first number: ");

int num1=sc.nextInt();
//this method reads value for num1 providing by user

System.out.print("Enter the second number: ");

int num2=sc.nextInt();
//this method reads value for num2 providing by user

sc.close();//closing the scanner class

int Product=calcProduct(num1,num2); //calling the method
System.out.println("product of two numbers  "+Product);
}
//calcProduct method
public static int calcProduct(int x,int y){
int result=x*y;
return result;//returning the result to main method


}
}

When the above code is compiled and executed, it produces the following results

Enter the first number: 12
Enter the second number: 23
product of two numbers 276

 

Program 3

Java program to multiply two floating numbers using the method

This is a program to calculate product of floating point values

import java.util.Scanner;
public class ProductOfNumbur2{

public static void main(String args[]){

Scanner sc=new Scanner(System.in);
// create scanner object

System.out.print("Enter the first number: ");

double num1=sc.nextDouble();
//this method reads value for num1 providing by user

System.out.print("Enter the second number: ");

double num2=sc.nextDouble();
//this method reads value for num2 providing by user

sc.close();//closing the scanner class

calcProduct(num1,num2); //calling the method

}
//calcTotal method
public static void calcProduct(double x,double y){
double result=x*y;
System.out.println("product of two numbers  "+result);
}
}

When the above code is compiled and executed, it produces the following results

Enter the first number:32.21
Enter the second number:12.1
Product of two numbers 389.741

In this program, the user is asked to enter two numbers(floating numbers). then it calculating product of those numbers using Java method and display on the screen.

Here, when the user enters the numbers, the first number stored in variable num1 and the second number stored the variable num2. Then, the product of the numbers is stored in the variable product.

Suggested for you

Java program operator

The data type in the Java language

The method in Java language

 

Cpp program to multiply two numbers using function
C program to add two numbers using function
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

9 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

9 months ago