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

subtract two number using method

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

Metod in Java language

Class and main method in Java

Data type and variable in Java language

 

Similar post

Subtract two numbers in Java language

Subtract two numbers in C language

Subtract two numbers in C++ language

Subtract two numbers in Python language

Python program to subtract two number using Function
Use of Java program to subtraction of two numbers using recursion
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

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 days ago

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