Practice Programs – Operators in Java
Beginner Level Programs (Arithmetic, Unary, Relational, Logical, Ternary)
Program 1: Addition of Two Numbers
Concept: Arithmetic operator (+)
Program:
class Addition
public static void main()
int a = 10;
int b = 5;
int sum = a + b;
[Link]("Sum = " + sum);
Output:
Sum = 15
Program 2: Subtraction Example
Concept: Arithmetic operator (-)
Program:
class Subtraction
public static void main()
int a = 20;
int b = 8;
int result = a - b;
[Link]("Result = " + result);
Output:
Result = 12
Program 3: Multiplication Example
Concept: Arithmetic operator (*)
Program:
class Multiplication
public static void main()
{
int a = 6;
int b = 4;
int product = a * b;
[Link]("Product = " + product);
Output:
Product = 24
Program 4: Division Example
Concept: Arithmetic operator (/)
Program:
class Division
public static void main()
int a = 20;
int b = 4;
int result = a / b;
[Link]("Division = " + result);
}
Output:
Division = 5
Program 5: Modulus Example
Concept: Arithmetic operator (%)
Program:
class ModulusExample
public static void main()
int a = 17;
int b = 5;
int remainder = a % b;
[Link]("Remainder = " + remainder);
Output:
Remainder = 2
Program 6: Increment Operator
Concept: Unary operator (++ )
Program:
class IncrementDemo
public static void main()
int a = 10;
a++;
[Link]("Value of a = " + a);
Output:
Value of a = 11
Program 7: Decrement Operator
Concept: Unary operator (--)
Program:
class DecrementDemo
public static void main()
{
int a = 10;
a--;
[Link]("Value of a = " + a);
Output:
Value of a = 9
Program 8: Relational Operator Example
Concept: Relational operator (>)
Program:
class RelationalExample
public static void main()
int a = 10;
int b = 5;
boolean result = a > b;
[Link](result);
}
Output:
true
Program 9: Logical Operator Example
Concept: Logical operator (&&)
Program:
class LogicalExample
public static void main()
int a = 10;
int b = 5;
boolean result = (a > 5) && (b < 10);
[Link](result);
Output:
true
Program 10: Ternary Operator Example
Concept: Ternary operator
Program:
class TernaryExample
public static void main()
int a = 10;
int b = 20;
int max = (a > b) ? a : b;
[Link]("Maximum number = " + max);
Output:
Maximum number = 20