-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.java
40 lines (33 loc) · 1.2 KB
/
app.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class Introduction {
// Entry point of the program
public static void main(String[] args) {
// Print a welcome message
System.out.println("Welcome to Java!");
// Declare and initialize variables
int number = 10;
String name = "Alice";
// Print the variables
System.out.println("Number: " + number);
System.out.println("Name: " + name);
// Use a loop to print numbers from 1 to 5
System.out.println("Printing numbers from 1 to 5:");
for (int i = 1; i <= 5; i++) {
System.out.print(i + " ");
}
System.out.println();
// Use a conditional statement to check if a number is even or odd
int checkNumber = 7;
if (checkNumber % 2 == 0) {
System.out.println(checkNumber + " is even.");
} else {
System.out.println(checkNumber + " is odd.");
}
// Call a function to calculate the square of a number
int squareResult = square(4);
System.out.println("Square of 4 is: " + squareResult);
}
// Function to calculate the square of a number
public static int square(int num) {
return num * num;
}
}