Advertisement
class A
{
public static void main(String...ar)
{
int x=20, y=20; // local variables
int sum = x + y; //addition of two local int variables
int difference = x-y; //subtraction of two local int variables
int multiplication = x * y; //multiplication of two local int variables
System.out.println("Sum of two int variables = "+ sum);
System.out.println("Subtraction of two int variables = "+ difference);
System.out.println("Multplication of two int variables = "+ multiplication);
}
}
Sum of two int variables = 40
Subtraction of two int variables = 0
Multplication of two int variables = 400
class A
{
int a=20; //instance variable
int b=30; //instance variable
int result;
public static void main(String... ar)
{
A ob= new A();
ob.result = ob.a + ob.b; //addition of two instance variables of type int
System.out.println("Sum of two instance variables = "+ ob.result);
ob.result = ob.a - ob.b; //subtraction of two instance variables of type int
System.out.println("Subtraction of two instance variables = "+ ob.result);
ob.result = ob.a * ob.b; //multiplication of two instance variables of type int
System.out.println("Multiplication of two instance variables = "+ ob.result);
}
}
Sum of two instance variables = 50
Subtraction of two instance variables = -10
Multiplication of two instance variables = 600
Advertisement
class A
{
int i=20;
int j=10;
public static void main(String... ar)
{
A ob = new A();
System.out.println("Quotient after dividing 20 by 10 : " + ob.i / ob.j);
System.out.println("Remainder after diviiding 20 by 10 : " + ob.i % ob.j);
int a = 90;
int b = 20;
System.out.println("Quotient after dividing 90 by 20 : "+ a / b);
System.out.println("Remainder after dividing 90 by 20 : "+ a % b);
}
}
Quotient after dividing 20 by 10 : 2
Remainder after diviiding 20 by 10 : 0
Quotient after dividing 90 by 20 : 4
Remainder after dividing 90 by 20 : 10
class A
{
public static void main(String... ar)
{
int result = 3+2-4+2; // + - operators have equal priorities
System.out.println(result);
result = 3+2*4+2; // + * + have unequal priorities
System.out.println(result);
result = 3+2*4/2; // + * / operators have unequal priorities
System.out.println(result);
result = 3%2*4/2; // % * / operators have equal priorities
System.out.println(result);
}
}
3
13
7
2
class A
{
public static void main(String... ar)
{
int result = (3+2)*4+2;
System.out.println(result);
result = 3%(2*4)/2;
System.out.println(result);
}
}
22
1
Advertisement
Advertisement
Please check our latest addition
PYTHON and DJANGO
Advertisement