Java program to multiply two number using function
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
10 best Ways to Subtract Two Numbers in Java (With Examples) In this article, we…
Array Data Structure: Definition, Types, Operations & Advantages Array Data Structure Introduction In this post,…
20 ways to subtract two numbers in Java In this article, we will discuss the…
10 simple ways to add two numbers in Java In this article, we will discuss…
Write a Python program to find the first n prime numbers In this article we…
Python: Calculate Average of odd and even in a list using loops In this post,…