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
Exercise instructions
- Save the result of
5plus6into theintcalledvarA. - Save the result of
varAminus3.5into thedoublecalledvarB. - Save the result of
3times3into theintcalledvarC. - Save the result of
varCdivided by3into theintcalledvarD1and also to adoublecalledvarD2.
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);
}
}