Get startedGet started for free

Performing calculations

Math is at the heart of programming - from simple addition to complex formulas. Java provides operators that let you perform calculations just like you would with a calculator. Let's put these operators into action!

This exercise is part of the course

Introduction to Java

View Course

Exercise instructions

  • Save the result of 5 plus 6 into the int called varA.
  • Save the result of varA minus 3.5 into the double called varB.
  • Save the result of 3 times 3 into the int called varC.
  • Save the result of varC divided by 3 into the int called varD1 and also to a double called varD2.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

class BasicMaths {
    public static void main(String[] args) {
    
    	// Save the result of 5 plus 6 into varA
        int varA = ____ ____ ____;
        
        // Save the result of varA minus 3.5 into varB
        double varB = ____;
        
        // Save the result of 3 times 3 into varC
        ____; 
        
        // Save the result of varC divided by 3 into varD1
        int varD1 = ____;
        
        // Save the result of varC divided by 3 into varD2
        ____;   
        
        System.out.println("int varD1: " + varD1);
        System.out.println("double varD2: " + varD2);
	}
}
Edit and Run Code