Write a Program that Display Subtract of Two Numbers

Share:

Write a Program that Display Subtract of Two Numbers

In this article, we will learn how to write that display Subtract of Two Numbers program of java. We can subtract two numbers java program. 
Java Program by TechnoNiit

Subtract of Two Numbers are simple program that outputs "4 - 2 = 2" on the screen. Since it's a very simple program, it's often used to introduce a new programming language to a newbie.

For Executing any java program, you need to

  • Install the JDK if you don't have installed it, download the JDK and install it.
  • Set path of the jdk/bin directory.
  • Create the java program
  • Compile and run the java program
//Subtract of Two Numbers Program in java

class ExampleSub
{
    public static void main(String args[])
    {
      int a=4;
      int b=2;
      int c=(a-b);

      //first way to print result
     System.out.println(c);

     //second way to print result
     System.out.println("Subtract of Two Numbers:"
     +a+" - "+b+" = "+c);
    }
}
//Subtract of Three Numbers Program in java

public class ExampleSub 
  {
    public static void main(String args[])
    {
      int a=8, b=4, c=2;
      int d=(a-b-c);

      //first way to print result
     System.out.println(d);

     //second way to print result
     System.out.println("Subtract of Three Numbers:"
     +a+" - "+b+" - "+c+" = "+d);
    }
  }

If you have copied the exact code, you need save the file name as ExampleSub.java. It's because the name of the class and filename should match in Java.

Compile: javac ExampleSub.java

Execute: java ExampleSub


You May Also Like:

Sum of Two Numbers in java

Multiply of Two Numbers in java

How Create Calculator in java

That’s it. Now you have successfully print Subtract of Two & Three Numbers Program in Java. If you have any doubt or question comment down below.

If you want to see the video of Subtract of two numbers, then watch the video below.



I hope this post will useful to you, Thanks For Visiting, Keep Visiting.

No comments