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

class and main method in Java

 

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

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago