HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

Java How To Add Two Numbers

Add Two Numbers

Learn how to add two numbers in Java:

Example
int a = 5;
int b = 6;
int sum = a + b;
System.out.println(sum );// Print the sum of a + b

Add Two Numbers with User Input

Learn how to add two numbers with user input:

Example
import java.util.Scanner; // Import the Scanner class 

class MyClass { 
  public static void main(String[] args) { 
    int a, b, sum; 
    Scanner myObj = new Scanner(System.in); // Create a Scanner object 
    System.out.println ("Type a number:"); 
    a  = myObj.nextInt(); // Read user input 

    System.out.println("Type another number:");
    b  = myObj.nextInt(); // Read user input 

    sum = a + b;  // Calculate the sum of a + b 
    System.out.println("Sum is: " + sum ); // Print the sum 
  }
}