The post Write a Java program to print the area and perimeter of a square appeared first on OctopusCodes.
]]>package octopuscodes.com.demo;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("a: ");
double a = scanner.nextDouble();
double area = a * a;
double perimeter = a * 4;
System.out.println("Area : " + area);
System.out.println("Perimeter : " + perimeter);
}
}
a: 5
Area : 25.0
Perimeter : 20.0
The post Write a Java program to print the area and perimeter of a square appeared first on OctopusCodes.
]]>The post Write a Java program to print the area and perimeter of a rectangle appeared first on OctopusCodes.
]]>package octopuscodes.com.demo;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("a: ");
double a = scanner.nextDouble();
System.out.print("b: ");
double b = scanner.nextDouble();
double area = a * b;
double perimeter = (a + b) * 2;
System.out.println("Area : " + area);
System.out.println("Perimeter : " + perimeter);
}
}
a: 3
b: 10
Area : 30.0
Perimeter : 26.0
The post Write a Java program to print the area and perimeter of a rectangle appeared first on OctopusCodes.
]]>The post Write a Java program to print the area and perimeter of a circle appeared first on OctopusCodes.
]]>package octopuscodes.com.demo;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Radius: ");
double radius = scanner.nextDouble();
double area = Math.PI * radius * radius;
double perimeter = 2 * Math.PI * radius;
System.out.println("Area : " + area);
System.out.println("Perimeter : " + perimeter);
}
}
Radius: 4
Area : 50.26548245743669
Perimeter : 25.132741228718345
The post Write a Java program to print the area and perimeter of a circle appeared first on OctopusCodes.
]]>The post Write a Java program to convert Celsius into Fahrenheit appeared first on OctopusCodes.
]]>package octopuscodes.com.demo;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Celsius: ");
double celsius = scanner.nextDouble();
double fahrenheit = celsius * 1.8 + 32;
System.out.println("Fahrenheit: " + fahrenheit);
}
}
Celsius: 100
Fahrenheit: 212.0
The post Write a Java program to convert Celsius into Fahrenheit appeared first on OctopusCodes.
]]>The post Write a Java program to convert Fahrenheit into Celsius appeared first on OctopusCodes.
]]>package octopuscodes.com.demo;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Fahrenheit: ");
double fahrenheit = scanner.nextDouble();
double celsius = (fahrenheit - 32) / 1.8;
System.out.println("Celsius: " + celsius);
}
}
Fahrenheit: 212
Celsius: 100.0
The post Write a Java program to convert Fahrenheit into Celsius appeared first on OctopusCodes.
]]>The post Java Operators with Real Life Examples appeared first on OctopusCodes.
]]>The post Java Operators with Real Life Examples appeared first on OctopusCodes.
]]>The post Assignment Operators in Java appeared first on OctopusCodes.
]]>| Operator | Example |
| = | c = d; |
| += | c += d; |
| -= | c -= d; |
| *= | c *= d; |
| /= | c /= d; |
| %= | c %= d; |
This operator is used to assign the value on the right to the variable on the left.
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
int a = 10;
System.out.println("a: " + a);
double b, c, d, e;
b = c = d = e = 4.5;
System.out.println("b: " + b);
System.out.println("c: " + c);
System.out.println("d: " + d);
System.out.println("e: " + e);
double f = b + c;
System.out.println("f: " + f);
}
}
a: 10
b: 4.5
c: 4.5
d: 4.5
e: 4.5
f: 9.0
This operator is a compound of “+” and “=” operators. It operates by adding the current value of the variable on the left to the value on the right and then assigning the result to the operand on the left.
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
int a = 10;
a += 5;
System.out.println("a: " + a);
}
}
a: 15
This operator is a compound of “-” and “=” operators. It operates by subtracting the variable’s value on the right from the current value of the variable on the left and then assigning the result to the operand on the left.
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
int a = 10;
a -= 5;
System.out.println("a: " + a);
}
}
a: 5
This operator is a compound of “*” and “=” operators. It operates by multiplying the current value of the variable on the left to the value on the right and then assigning the result to the operand on the left.
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
int a = 10;
a *= 5;
System.out.println("a: " + a);
}
}
a: 50
This operator is a compound of “/” and “=” operators. It operates by dividing the current value of the variable on the left by the value on the right and then assigning the quotient to the operand on the left.
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
int a = 10;
a /= 5;
System.out.println("a: " + a);
}
}
a: 2
This operator is a compound of “%” and “=” operators. It operates by dividing the current value of the variable on the left by the value on the right and then assigning the remainder to the operand on the left.
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
int a = 10;
a %= 5;
System.out.println("a: " + a);
}
}
a: 0
The post Assignment Operators in Java appeared first on OctopusCodes.
]]>The post Logical Operators in Java appeared first on OctopusCodes.
]]>| Operator | Description |
| && (Logical AND) | Returns true if both statements are true. |
| || (Logical OR) | Returns true if one of the statements is true. |
| ! (Logical NOT) | Reverse the result, returns false if the result is true and vice versa. |
This operator returns true when both the conditions under consideration are satisfied or are true. If even one of the two yields false, the operator results false.
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
int a = 10, b = 5;
boolean result1 = a > 3 && b > 2;
System.out.println("result 1: " + result1);
boolean result2 = a > 30 && b > 2;
System.out.println("result 2: " + result2);
}
}
result 1: true
result 2: false
This operator returns true when one of the two conditions under consideration is satisfied or is true. If even one of the two yields true, the operator results true. To make the result false, both the constraints need to return false.
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
int a = 10, b = 5;
boolean result1 = a > 3 || b > 2;
System.out.println("result 1: " + result1);
boolean result2 = a > 30 || b < 2;
System.out.println("result 2: " + result2);
}
}
result 1: true
result 2: false
This operator is a unary operator and returns true when the condition under consideration is not satisfied or is a false condition.
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
int a = 10, b = 5;
boolean result1 = !(a > 3 || b > 2);
System.out.println("result 1: " + result1);
boolean result2 = !(a > 30 || b < 2);
System.out.println("result 2: " + result2);
}
}
result 1: false
result 2: true
The post Logical Operators in Java appeared first on OctopusCodes.
]]>The post Relational Operators in Java appeared first on OctopusCodes.
]]>| Operator | Description |
| == | Is Equal To |
| != | Not Equal To |
| > | Greater Than |
| >= | Greater Than or Equal To |
| < | Less Than |
| <= | Less Than or Equal To |
This operator is used to check whether the two given operands are equal or not. The operator returns true if the operand at the left-hand side is equal to the right-hand side, else false.
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
int a = 10, b = 5;
boolean result1 = a == b;
System.out.println("result 1: " + result1);
int c = 7, d = 7;
boolean result2 = c == d;
System.out.println("result 2: " + result2);
}
}
result 1: false
result 2: true
This operator is used to check whether the two given operands are equal or not. It returns true if the operand at the left-hand side is not equal to the right-hand side, else false.
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
int a = 10, b = 5;
boolean result1 = a != b;
System.out.println("result 1: " + result1);
int c = 7, d = 7;
boolean result2 = c != d;
System.out.println("result 2: " + result2);
}
}
result 1: true
result 2: false
This checks whether the first operand is greater than the second operand or not. The operator returns true when the operand at the left-hand side is greater than the right-hand side.
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
int a = 10, b = 5;
boolean result1 = a > b;
System.out.println("result 1: " + result1);
int c = 7, d = 17;
boolean result2 = c > d;
System.out.println("result 2: " + result2);
}
}
result 1: true
result 2: false
This checks whether the first operand is greater than or equal to the second operand or not. The operator returns true when the operand at the left-hand side is greater than or equal to the right-hand side.
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
int a = 10, b = 5;
boolean result1 = a >= b;
System.out.println("result 1: " + result1);
int c = 7, d = 7;
boolean result2 = c >= d;
System.out.println("result 2: " + result2);
int e = 7, f = 14;
boolean result3 = e >= f;
System.out.println("result 3: " + result3);
}
}
result 1: true
result 2: true
result 3: false
This checks whether the first operand is less than the second operand or not. The operator returns true when the operand at the left-hand side is less than the right-hand side.
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
int a = 10, b = 5;
boolean result1 = a < b;
System.out.println("result 1: " + result1);
int c = 7, d = 17;
boolean result2 = c < d;
System.out.println("result 2: " + result2);
}
}
result 1: false
result 2: true
This checks whether the first operand is greater than or equal to the second operand or not. The operator returns true when the operand at the left-hand side is greater than or equal to the right-hand side.
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
int a = 10, b = 5;
boolean result1 = a <= b;
System.out.println("result 1: " + result1);
int c = 7, d = 7;
boolean result2 = c <= d;
System.out.println("result 2: " + result2);
int e = 2, f = 7;
boolean result3 = e <= f;
System.out.println("result 3: " + result3);
}
}
result 1: false
result 2: true
result 3: true
The post Relational Operators in Java appeared first on OctopusCodes.
]]>The post Unary Operators in Java appeared first on OctopusCodes.
]]>| Operator | Description |
| + | Unary plus operator; indicates positive value |
| – | Unary minus operator; negates an expression |
| ++ | Increment operator; increments a value by 1 |
| – – | Decrement operator; decrements a value by 1 |
| ! | Logical complement operator; inverts the value of a boolean |
It is used to represent positive values. Usually, we do not write the operator before the operand. Hence, it is optional.
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
int number1 = 4;
System.out.println("Number 1: " + number1);
int result1 = +number1;
System.out.println("After unary plus operation result 1 is " + result1);
int number2 = -9;
System.out.println("Number 2: " + number2);
int result2 = +number2;
System.out.println("After unary plus operation result 2 is " + result2);
}
}
Number 1: 4
After unary plus operation result 1 is 4
Number 2: -9
After unary plus operation result 2 is -9
It is used to convert a positive value into a negative one.
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
int number1 = 4;
System.out.println("Number 1: " + number1);
int result1 = -number1;
System.out.println("After unary minus operation result 1 is " + result1);
int number2 = -9;
System.out.println("Number 2: " + number2);
int result2 = -number2;
System.out.println("After unary minus operation result 2 is " + result2);
}
}
Number 1: 4
After unary minus operation result 1 is -4
Number 2: -9
After unary minus operation result 2 is 9
It is used to increment the value of an operand by one. The operator can be applied before or after an operand.
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
int number = 4;
System.out.println("Number: " + number);
++number;
System.out.println("After Pre-increment operation number is " + number);
number++;
System.out.println("After Post-increment operation number is " + number);
}
}
Number: 4
After Pre-increment operation number is 5
After Post-increment operation number is 6
It is used to decrement the value of an operand by 1. The operator can be applied before or after an operand.
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
int number = 4;
System.out.println("Number: " + number);
--number;
System.out.println("After Pre-decrement operation number is " + number);
number--;
System.out.println("After Post-decrement operation number is " + number);
}
}
Number: 4
After Pre-decrement operation number is 3
After Post-decrement operation number is 2
A logical Complement Operator is used to reverse the value of a Boolean value. It means that if the value of an operand is true, then the complement of the operator will be false and vice-versa.
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
boolean status1 = true;
System.out.println("Status1: " + status1);
status1 = !status1;
System.out.println("After logical complement operation status 1 is " + status1);
boolean status2 = false;
System.out.println("Status2: " + status2);
status2 = !status2;
System.out.println("After logical complement operation status 2 is " + status2);
}
}
Status1: true
After logical complement operation status 1 is false
Status2: false
After logical complement operation status 2 is true
The post Unary Operators in Java appeared first on OctopusCodes.
]]>