Java program to subtract two number using method
- Home
- Calculations
- Java program to subtract two number using method
- On
- By
- 0 Comment
- Categories: Calculations
Java program to subtract two number using method
Java program to subtract two number using method
In this tutorial, we will discuss the Java program to subtract two number using method
In this topic, we are going to learn how to subtract two numbers (integer, floating point) using the method in Java language
already we are learned the same concept using the operator
if you want to remember click here, Java program to subtract two numbers
Approach
- Variable declaration
- Read value from user
- method definition with parameter
- call the method with argument
- display result on the screen
Java program to subtract two number
Code to subtract two integer number using method
The program allows to enter two integer values then, it calculates subtraction of the given two numbers
Program 1
import java.util.Scanner; public class SubTwoNumbers{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); //create a scanner instance for receives input // from the user - input from keyboard System.out.print("Enter Two numbers for subtraction: "); int num1=scan.nextInt(); int num2=scan.nextInt(); //reads the numbers from the user //input from keyword calcSub(num1,num2);//call the metohd } static void calcSub(int x, int y){ //Define a user defined method System.out.println("Subtraction of two given numbers: "+(x-y)); //display the subtraction of the numbers } }
When the above code is executed, it produces the following result
case 1
Enter two numbers for subtraction: 125 475 Difference of two numbers: -350
Case 2
Enter two numbers for subtraction: 550 340 Subtraction of two given numbers: 210
Code to subtract two number in method with return
The program allows to enter two integer values then, it calculates subtraction of the given numbers using return keyword
Program 2
import java.util.Scanner; public class SubTwoNumbers1{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); //create a scanner instance for receives input // from the user - input from keyboard System.out.print("Enter Two numbers for subtraction: "); int num1=scan.nextInt(); int num2=scan.nextInt(); //reads the numbers from the user //input from keyword int subNum=calcSub(num1,num2);//call the metohd System.out.println("Subtraction of two given numbers: "+subNum); } static int calcSub(int x, int y){ //Define a user defined method int result=(x-y); return result; } }
When the above code is executed, it produces the following result
case 1
Enter two numbers for subtraction: 1000 500 subtraction of two given numbers: 500
Case 2
Enter two numbers for subtraction: 400 550 subtraction of two given numbers: 150
Code to subtract two floating point numbers
The program allows to enter two float values then, it calculates subtraction of the given numbers
Program 3
import java.util.Scanner; public class SubTwoNumbers2{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); //create a scanner instance for receives input // from the user - input from keyboard System.out.print("Enter Two numbers for subtraction: "); double num1=scan.nextDouble(); double num2=scan.nextDouble(); //reads the numbers from the user //input from keyword calcSub(num1,num2);//call the metohd } static void calcSub(double x, double y){ //Define a user defined method System.out.println("Subtraction of two numbers: "+(x-y)); //display the sum of the numbers } }
When the above code is executed, it produces the following result
Enter two numbers for subtraction: 345.66 34.55 subtraction of two given numbers: 311.11
Suggetsed for you
Data type and variable in Java language
Similar post
Subtract two numbers in Java language
Subtract two numbers in C language