Write a Program that Display Sum of Two Numbers

Share:

Write a Program that Display Sum of Two Number

In this article, we will learn how to write Sum of Two Numbers program of java. We can add two numbers java program.

Java Programs by TechnoNiit

Sum of Two Numbers are simple program that outputs "2 + 2 = 4" 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

//Sum of Two Numbers Program in java

class ExampleSum
{
 public static void main(String args[])
 {
 int a=2;
 int b=2;
 int c=(a+b);

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

 //Second way to print
 System.out.println("Sum of Two Numbers: "+a+" + "+b+" = "+c);
 }
}

//Sum of Three Numbers Program in java

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

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

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

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


Compile:  javac ExampleSum.java

Execute:  java ExampleSum

You May Also Like:

First Program in java | Hello World

Subtract of Two Numbers in Java

How to Create Calculator in Java 


That’s it. Now you have successfully print Sum 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 Sum of two numbers, then watch the video below.



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

No comments