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
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
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
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
How to find reverse number using method In this article, we will discuss the concept…
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…