Addition of two numbers in Java Programming Language

Avatar

 BY TEJAS




Java is the Most Popular Programming language in the world. When we start learning java there is a basic example is to add two numbers in java. Java is nearly 100% object-oriented language.



In this example, we use java's basic classes this is a very simple example.
We are adding basic class public class demo this is very important as you know. We written demo here basically there is no any compulsion to write demo we have to only permission to write our file name their If you are change their name it gives you error. Then we add public static void main(String[] args) in pair of curly brackets {}.
Then we assign int to num1 for storing 5 and similarly the second int for storing 20 in num2 and third int for getting answer of 5 & 20.

then we write System.out.println to write the output on screen.

It is very simple example which is basic example while learning Java language.

Add 2 Numbers in Java

In below video shows program how works


below here is a Java code and output of Java program.


Always remember extension of Java Program is .java


demo.java :-
    
        public class demo {
            public static void main(String[] args) {
        
                int num1 = 5;
                int num2 = 20;
                int sum = num1 + num2;
                System.out.println("The sum is: " + sum);
            }
        }
    


output :-
    
        The sum is: 25

        Process finished with exit code 0
        
    
  
    
        public class demo {
            public static void main(String[] args) {
        
                int num1 = 5;
                int num2 = 20;
                int sum = num1 + num2;
                System.out.println("The sum is: " + sum);
            }
        }
    


output :-
    
     
        The sum is: 25

        Process finished with exit code 0
        
    
  
Search
Advertisement
Share With
Follow us on
Advertisement