Write a program to Subtract two numbers in java



In this core java programming tutorial we will write a program to Subtract two numbers in java.





Program / Example 1 > Write a program to Subtract two numbers in java>
/*
* Write a program to Subtract two numbers in java
*/
public class SubtractTwoNumbersInJava {
   public static void main(String[] args) {
          int firstNumber = 5;
          int secondNumber = 2;
          System.out.println("firstNumber - secondNumber = "
                       + (firstNumber - secondNumber));
   }
}
/*OUTPUT
firstNumber - secondNumber = 3
*/


Program / Example 2 > Write a program to Subtract two numbers in java, accept user's input >

import java.util.Scanner;
/*
* Write a program to subtract two numbers in java, user must be able to enter the number
*/
public class SubtractTwoNumbersInJavaFromUsersInput {
   public static void main(String[] args) {
          Scanner scan = new Scanner(System.in);
          System.out.print("enter first Number  : ");
          int firstNumber = scan.nextInt();
          System.out.print("enter second Number  : ");
          int secondNumber = scan.nextInt();
          System.out.println("firstNumber - secondNumber = "
                       + (firstNumber - secondNumber));
   }
}
/*OUTPUT
enter first Number  : 4
enter second Number  : 1
firstNumber - secondNumber = 3
*/

So, in this core java programming tutorial we wrote a program to Subtract two numbers in java.


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>

>Pattern/Pyramid generating programs



No comments:

Post a Comment