You are on page 1of 9

NAME: GHULE SAKSHI SANJAY

ROLL NO: SEA113 DIV: A


BRANCH: COMPUTER ENGINEERING

Q. Write a program to demonstrates


a) Relational Operators
b) Boolean and Logical operators
c) Shift operators and unsigned shift operators
d) Inbuilt Math functions
e) Type Casting

***RELATIONAL OPERATORS EXAMPLE***

The result of all relational operators is always of a boolean type. It returns always true or
false.
Relational operators are mainly used to create conditions in decision statements
The six relational operators are < , > , <= , >= , == , and != .

class Raltional {

public static void main(String[] args) {

//initializing the variables


int a = 10;
int b = 20;

//Declaring Relational operators


//Equal to operator
System.out.println("a == b = " + (a == b) );

//not equal to operator


System.out.println("a != b = " + (a != b) );

//Greater than operator


System.out.println("a > b = " + (a > b) );

//Lessthan operator
System.out.println("a < b = " + (a < b) );

//Greater than or equal to operator


System.out.println("b >= a = " + (b >= a) );

//Less than or equal to operator


System.out.println("b <= a = " + (b <= a) );
}
}
OUTPUT
***Boolean and logical operators***
Logical operators produce results or outputs in the form of boolean values i.e., either true or
false.
Boolean logical operators operate only on boolean operands.Logical operators are &(logical
AND), |(logical OR), !=(logical NOT), ^(logical XOR),
&&(short circuit AND), ||(short circuit OR), ==(equal to).....

class BoolLogic
{
public static void main(String[] args)
{ //initializing the values of variables
boolean a = true;
boolean b = false;
boolean c = a | b;
boolean d = a & b;
boolean e = (!a & b) | (a & !b);
boolean f = a ^ b;

//Performing Boolean and Logical operations


System.out.println(" a|b = " + c);
System.out.println(" a&b = " + d);
System.out.println(" !a&b|a&!b = " + e);
System.out.println("a^b = " + f);
}
}
OUTPUT
***shift operators***
There are two types of shift operators in Java. They are:
1. Left shift operator
2. Right shift operator
The operator that shifts the bits of number towards left by n number of bit positions is called
left shift operator in Java.
Shifting a value to the left, n bits, is equivalent to multiplying that value by 2^n.
For example: n = 3. So, 20 * 2^3 = 20 * 8 = 160

class leftshift{
public static void main(string[] args){
//initializing variables
int a = 10, b = 0, b = a<<2;
//Declaring left shift operator result
System.out.println("a<<2 = " + b);
}
}
OUTPUT

1. Signed right shift operator (>>)


2. Unsigned right shift operator (>>>)
Both of these operators shifts bits of number towards right by n number of bit positions.
Shifting a value to the right is equivalent to dividing the number by 2^n.

class rightshift{
public static void main(string[] args){
//initializing variables
int a = -10, b = 0, b = a>>2, c = 0, c = a>>>2;

//Declaring left shift operator result


System.out.println("a>>2 = " + b);
System.out.println("a>>>2 = " + c);
}
}

***Inbuilt math functions***


Java Math class provides several methods to work on math calculations like min(), max(),
avg(), sin(), cos(), tan(), round(), ceil(), floor(), abs() etc.

class Inbuilt{

Public static void main(string[] args){


//initializing the varibles
double x = 28;
double y = 4;

//performing math functions


//Returns the maximum value out of x and y
System.out.println("max out of x and y: " + Math.max(x,y));

//Returns the the value of of first when raised to the power of second
System.out.println("power of x and y: " + Math.pow(x,y));

//Returns the squareroot of y


System.out.println("sqrt of y: " + Math.sqrt(y));
}
}
OUTPUT
***Type casting***
Type casting is a method or process that converts a data type into another data type
There are two types of type casting:
1. Widening Type Casting
2. Narrowing Type Casting
class TypeCasting{

Public static void main(string[] args){


//initializing the variable
long x = 5;
System.out.println("***Widening Type Casting***");
float y = x;
double z = y;
System.out.println("before conversion: " + x);
System.out.println("conversion from long to float: " + y);
System.out.println("conversion from float to double: " + z);
System.out.println("***Narrowing Type Casting***");
int a = (int)x;
char b = (char)a;
System.out.println("conversion from long to int: " + a);
System.out.println("conversion from int to char: " + b);
}
}
OUTPUT

You might also like