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
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
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
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
The data type in the Java language
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…