Here are some simple Java program examples with explanations:
1. Hello, World Program
This is the most basic Java program that prints "Hello, World!" to the console.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Explanation:
public class HelloWorld
: Defines the class namedHelloWorld
.public static void main(String[] args)
: This is the entry point of the program. Themain
method is executed when the program runs.System.out.println("Hello, World!");
: Prints the text to the console.
2. Sum of Two Numbers
This program takes two numbers and calculates their sum.
import java.util.Scanner;
public class SumTwoNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();
int sum = num1 + num2;
System.out.println("The sum is: " + sum);
scanner.close();
}
}
Explanation:
Scanner scanner = new Scanner(System.in);
: Creates anScanner
object to read input from the user.nextInt()
: Reads an integer input.int sum = num1 + num2;
: Adds the two numbers.scanner.close();
: Closes the scanner to free resources.
3. Check if a Number is Even or Odd
This program determines if a given number is even or odd.
import java.util.Scanner;
public class EvenOdd {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (num % 2 == 0) {
System.out.println(num + " is Even.");
} else {
System.out.println(num + " is Odd.");
}
scanner.close();
}
}
Explanation:
if (num % 2 == 0)
: Checks if the number is divisible by 2 (even).else
: Executes if the number is not even, indicating it is odd.
4. Factorial of a Number
This program calculates the factorial of a number using a loop.
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int factorial = 1;
for (int i = 1; i <= num; i++) {
factorial *= i;
}
System.out.println("The factorial of " + num + " is " + factorial);
scanner.close();
}
}
Explanation:
for (int i = 1; i <= num; i++)
: Loops from 1 to the input number.factorial *= i;
: Multipliesfactorial
by the current value ofi
in each iteration.
5. Prime Number Check
This program checks if a number is prime.
import java.util.Scanner;
public class PrimeCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
boolean isPrime = true;
if (num <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
System.out.println(num + " is a prime number.");
} else {
System.out.println(num + " is not a prime number.");
}
scanner.close();
}
}
Explanation:
if (num % i == 0)
: Checks ifnum
is divisible byi
, indicating it's not prime.Math.sqrt(num)
: Optimizes the loop to run only up to the square root ofnum
.
6. Reverse a String
This program reverses an input string.
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
String reversed = "";
for (int i = input.length() - 1; i >= 0; i--) {
reversed += input.charAt(i);
}
System.out.println("Reversed string: " + reversed);
scanner.close();
}
}
Explanation:
input.length()
: Gets the length of the string.input.charAt(i)
: Accesses the character at thei
th position in the string.- The loop iterates in reverse to build the reversed string.
7. Fibonacci Sequence
This program generates the Fibonacci sequence up to n
terms.
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = scanner.nextInt();
int a = 0, b = 1;
System.out.print("Fibonacci Sequence: " + a + " " + b);
for (int i = 3; i <= n; i++) {
int next = a + b;
System.out.print(" " + next);
a = b;
b = next;
}
scanner.close();
}
}
Explanation:
- The first two terms of the Fibonacci sequence are
0
and1
. - The next term is calculated by adding the previous two terms.
These examples cover a range of common Java programming tasks. Let me know if you'd like more advanced examples! 😊