Java Code Collection
1. Hello World
public class Main {
public static void main(String[] args) {
[Link]("Hello World");
}
}
2. Positive / Negative / Zero
public class Main {
public static void main(String[] args) {
int n = 15;
if (n > 0) {
[Link]("Positive Number");
} else if (n < 0) {
[Link]("Negative Number");
} else {
[Link]("Zero");
}
}
}
3. Sum of Two Numbers
import [Link];
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a = [Link]();
int b = [Link]();
int sum = a + b;
[Link]("Sum = " + sum);
[Link]();
}
}
4. Variable Swapping
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 20;
int temp = a;
a = b;
b = temp;
[Link]("a = " + a);
[Link]("b = " + b);
}
}
5. Rectangle Area
import [Link];
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
double length = [Link]();
double width = [Link]();
double area = length * width;
[Link]("Area = " + area);
[Link]();
}
}