0% found this document useful (0 votes)
2 views2 pages

Java Codes Collection

The document contains Java code examples demonstrating basic programming concepts. It includes examples for printing 'Hello World', determining if a number is positive, negative, or zero, summing two numbers, swapping variables, and calculating the area of a rectangle. Each example is structured with a main class and method, showcasing fundamental Java syntax and functionality.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Java Codes Collection

The document contains Java code examples demonstrating basic programming concepts. It includes examples for printing 'Hello World', determining if a number is positive, negative, or zero, summing two numbers, swapping variables, and calculating the area of a rectangle. Each example is structured with a main class and method, showcasing fundamental Java syntax and functionality.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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]();
}
}

You might also like