Simple Java Programs
Java, a flexible and extensively used programming language, serves various domains. For Java beginners, here are five straightforward simple programs that introduce essential concepts and ensure a seamless initiation into the language.
1. Even or Odd Checker:
This program determines whether a given number is even or odd.
import java.util.Scanner;
public class EvenOrOdd {
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 an even number.");
} else {
System.out.println(num + " is an odd number.");
}
scanner.close();
}
}
Output:
Enter a number: 7
7 is an odd number.
2. Celsius to Fahrenheit Converter:
This program converts temperature from Celsius to Fahrenheit.
import java.util.Scanner;
public class CelsiusToFahrenheit {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter temperature in Celsius: ");
double celsius = scanner.nextDouble();
double fahrenheit = (celsius * 9/5) + 32;
System.out.println("Temperature in Fahrenheit: " + fahrenheit);
scanner.close();
}
}
Output:
Enter temperature in Celsius: 25
Temperature in Fahrenheit: 77.0
3. Palindrome Checker:
This program checks if a given word is a palindrome (reads the same forwards and backwards).
import java.util.Scanner;
public class PalindromeChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a word: ");
String word = scanner.nextLine();
String reversedWord = new StringBuilder(word).reverse().toString();
if (word.equalsIgnoreCase(reversedWord)) {
System.out.println("It's a palindrome!");
} else {
System.out.println("It's not a palindrome.");
}
scanner.close();
}
}
Output:
Enter a word: radar
It's a palindrome!
4. Sum of Natural Numbers:
This program calculates the sum of natural numbers up to a given number.
import java.util.Scanner;
public class SumOfNaturalNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int sum = 0;
for (int i = 1; i <= num; i++) {
sum += i;
}
System.out.println("Sum of natural numbers up to " + num + " is: " + sum);
scanner.close();
}
}
Output:
Enter a number: 5
Sum of natural numbers up to 5 is: 15
5. Factorial of a Number (Recursive):
This program calculates the factorial of a given number using a recursive function.
import java.util.Scanner;
public class RecursiveFactorial {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int factorial = calculateFactorial(num);
System.out.println("Factorial of " + num + " is: " + factorial);
scanner.close();
}
public static int calculateFactorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * calculateFactorial(n - 1);
}
}
}
Output:
Enter a number: 4
Factorial of 4 is: 24
Read More: 15 Basic Java Programs
Related Articles:
Top Java Programs asked in Interviews
Top Core Java Interview Questions
Top Java Collections Interview Questions