Java is one of the most popular programming languages, known for its versatility, portability, and wide range of applications. Whether you’re a beginner or an experienced developer, practicing basic Java programs is essential to understanding the language’s core concepts. In this blog, we’ll explore 10 basic Java programs with examples and their corresponding outputs. These programs cover fundamental topics like variables, loops, conditionals, arrays, and more. Let’s dive in!
1. Hello World Program
The “Hello World” program is the simplest Java program and is often the first step for beginners.
Code:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Output:
Hello, World!
Explanation:
public class HelloWorld
: Defines a class namedHelloWorld
.public static void main(String[] args)
: The main method, which is the entry point of the program.System.out.println("Hello, World!");
: Prints the text “Hello, World!” to the console.
2. Addition of Two Numbers
This program demonstrates how to perform basic arithmetic operations in Java.
Code:
public class AddNumbers {
public static void main(String[] args) {
int num1 = 5;
int num2 = 10;
int sum = num1 + num2;
System.out.println("Sum: " + sum);
}
}
Output:
Sum: 15
Explanation:
int num1 = 5;
andint num2 = 10;
: Declare and initialize two integer variables.int sum = num1 + num2;
: Adds the two numbers and stores the result insum
.System.out.println("Sum: " + sum);
: Prints the result.
3. Check if a Number is Even or Odd
This program checks whether a given number is even or odd.
Code:
public class EvenOdd {
public static void main(String[] args) {
int num = 7;
if (num % 2 == 0) {
System.out.println(num + " is even.");
} else {
System.out.println(num + " is odd.");
}
}
}
Output:
7 is odd.
Explanation:
if (num % 2 == 0)
: Checks if the number is divisible by 2.- If true, the number is even; otherwise, it’s odd.
4. Find the Largest of Two Numbers
This program compares two numbers and prints the larger one.
Code:
public class LargestNumber {
public static void main(String[] args) {
int num1 = 12;
int num2 = 15;
if (num1 > num2) {
System.out.println(num1 + " is larger.");
} else {
System.out.println(num2 + " is larger.");
}
}
}
Output:
15 is larger.
Explanation:
- Compares
num1
andnum2
using anif-else
statement. - Prints the larger number.
5. Factorial of a Number
This program calculates the factorial of a number using a loop.
Code:
public class Factorial {
public static void main(String[] args) {
int num = 5;
int factorial = 1;
for (int i = 1; i <= num; i++) {
factorial *= i;
}
System.out.println("Factorial of " + num + " is: " + factorial);
}
}
Output:
Factorial of 5 is: 120
Explanation:
- Uses a
for
loop to multiply numbers from 1 tonum
. - Stores the result in
factorial
.
6. Fibonacci Series
This program prints the Fibonacci series up to a specified number of terms.
Code:
public class Fibonacci {
public static void main(String[] args) {
int n = 10, firstTerm = 0, secondTerm = 1;
System.out.println("Fibonacci Series:");
for (int i = 1; i <= n; i++) {
System.out.print(firstTerm + " ");
int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
}
}
}
Output:
Fibonacci Series:
0 1 1 2 3 5 8 13 21 34
Explanation:
- Uses a loop to generate the Fibonacci sequence.
- Each term is the sum of the two preceding ones.
7. Reverse a Number
This program reverses a given number.
Code:
public class ReverseNumber {
public static void main(String[] args) {
int num = 1234, reversed = 0;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
System.out.println("Reversed Number: " + reversed);
}
}
Output:
Reversed Number: 4321
Explanation:
- Uses a
while
loop to extract digits and reverse the number.
8. Check if a Number is Prime
This program checks whether a number is prime.
Code:
public class PrimeNumber {
public static void main(String[] args) {
int num = 29;
boolean isPrime = true;
for (int i = 2; i <= num / 2; 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.");
}
}
}
Output:
29 is a prime number.
Explanation:
- Checks divisibility of
num
by numbers from 2 tonum/2
. - If divisible,
num
is not prime.
9. Sum of Array Elements
This program calculates the sum of elements in an array.
Code:
public class SumArray {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int sum = 0;
for (int num : arr) {
sum += num;
}
System.out.println("Sum of array elements: " + sum);
}
}
Output:
Sum of array elements: 15
Explanation:
- Uses a
for-each
loop to iterate through the array and calculate the sum.
10. Palindrome Check
This program checks if a string is a palindrome.
Code:
public class Palindrome {
public static void main(String[] args) {
String str = "madam";
String reversed = new StringBuilder(str).reverse().toString();
if (str.equals(reversed)) {
System.out.println(str + " is a palindrome.");
} else {
System.out.println(str + " is not a palindrome.");
}
}
}
Output:
madam is a palindrome.
Explanation:
- Reverses the string and compares it with the original.
Conclusion
These basic Java programs are a great starting point for beginners to understand the language’s syntax and logic. By practicing these examples, you’ll build a strong foundation in Java programming. As you progress, you can explore more advanced topics like object-oriented programming, data structures, and algorithms.
Found this blog helpful? Share it with your friends and leave a comment below with your favorite Java program!