OneBite.Dev - Coding blog in a bite size

add two numbers in java

Code snippet on how to add two numbers in java

  public class AddTwoNumbers { 
   
   public static void main(String[] args) { 
       
      int num1 = 5, num2 = 15, sum;
      sum = num1 + num2;

      System.out.println("Sum of these numbers: "+sum);
   }
}

This code creates a class called AddTwoNumbers. Inside this class, there is a main method that takes an array of strings (args) as a parameter. We then declare two integer variables named num1 and num2, setting them to 5 and 15 respectively. We then create a third variable named sum and set it equal to the sum of num1 and num2. Finally, we print out the sum of the two variables. This program outputs “Sum of these numbers: 20”.

java