Basic Java Program Examples With Outputs
Here are some basic Java program examples with outputs:
1) Hello World Program:
public class HelloWorld {
public static void main(String[] args) {
[Link](“Hello, World!”);
Output:
Hello, World!
2) Addition of Two Numbers:
public class AddNumbers {
public static void main(String[] args) {
int num1 = 5, num2 = 10, sum;
sum = num1 + num2;
[Link](“Sum of ” + num1 + ” and ” + num2 + ” is: ” + sum);
Output:
Sum of 5 and 10 is: 15
3) Find Maximum of Three Numbers:
public class MaxOfThreeNumbers {
public static void main(String[] args) {
int num1 = 10, num2 = 20, num3 = 15, max;
max = (num1 > num2) ? (num1 > num3 ? num1 : num3) : (num2 > num3 ? num2 :
num3);
[Link](“Maximum of ” + num1 + “, ” + num2 + “, and ” + num3 + ” is: ” +
max);
Output:
Maximum of 10, 20, and 15 is: 20
4) Check Even or Odd Number:
public class EvenOdd {
public static void main(String[] args) {
int num = 5;
if(num % 2 == 0)
[Link](num + ” is even.”);
else
[Link](num + ” is odd.”);
Output:
5 is odd.
5) Factorial of a Number:
public class Factorial {
public static void main(String[] args) {
int num = 5, factorial = 1;
for(int i = 1; i <= num; ++i) {
factorial *= i;
}
[Link](“Factorial of ” + num + ” is: ” + factorial);
Output:
Factorial of 5 is: 120
6) Print Pattern in Java:
public class PrintPattern {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; ++i) {
for (int j = 1; j <= i; ++j) {
[Link](“* “);
[Link]();
Output:
**
***
****
*****
7) Multiply Two Numbers in Java:
public class MultiplyTwoNumbers {
public static void main(String[] args) {
double first = 2.5, second = 4.5;
double product = first * second;
[Link](“The product is: ” + product);
Output:
The product is: 11.25
8) Check Leap Year in Java:
public class LeapYear {
public static void main(String[] args) {
int year = 2024;
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
[Link](year + ” is a leap year.”);
} else {
[Link](year + ” is not a leap year.”);
Output:
2024 is a leap year.
9) Check Vowel or Consonant in Java:
public class VowelConsonant {
public static void main(String[] args) {
char ch = „A‟;
if (ch == „a‟ || ch == „e‟ || ch == „i‟ || ch == „o‟ || ch == „u‟
|| ch == „A‟ || ch == „E‟ || ch == „I‟ || ch == „O‟ || ch == „U‟) {
[Link](ch + ” is a vowel.”);
} else {
[Link](ch + ” is a consonant.”);
Output:
A is a vowel.
10) Java Program to Calculate Simple Interest:
import [Link];
public class SimpleInterest {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link](“Enter principal amount: “);
double principal = [Link]();
[Link](“Enter rate of interest: “);
double rate = [Link]();
[Link](“Enter time period in years: “);
double time = [Link]();
double simpleInterest = (principal * rate * time) / 100;
[Link](“Simple Interest: ” + simpleInterest);
[Link]();
}
Output:
Enter principal amount: 5000
Enter rate of interest: 2.5
Enter time period in years: 3
Simple Interest: 375.0
11) Java Program to Find Quotient and Remainder:
import [Link];
public class QuotientRemainder {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link](“Enter dividend: “);
int dividend = [Link]();
[Link](“Enter divisor: “);
int divisor = [Link]();
int quotient = dividend / divisor;
int remainder = dividend % divisor;
[Link](“Quotient: ” + quotient);
[Link](“Remainder: ” + remainder);
[Link]();
Output:
Enter dividend: 17
Enter divisor: 5
Quotient: 3
Remainder: 2
12) Java Program to Calculate Power of a Number:
import [Link];
public class PowerOfNumber {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link](“Enter base: “);
int base = [Link]();
[Link](“Enter exponent: “);
int exponent = [Link]();
long result = 1;
while (exponent != 0) {
result *= base;
–exponent;
[Link](“Result: ” + result);
[Link]();
Output:
Enter base: 3
Enter exponent: 4
Result: 81
13) Java Program to Convert char to String and String to Char:
public class CharStringConversion {
public static void main(String[] args) {
// Convert char to String
char ch = „A‟;
String str = [Link](ch);
[Link](“Char to String: ” + str);
// Convert String to char
String s = “Hello”;
char c = [Link](0);
[Link](“String to Char: ” + c);
14) Java Program to Sort Strings in Alphabetical Order:
import [Link];
public class SortStrings {
public static void main(String[] args) {
String[] strings = {“orange”, “apple”, “banana”, “grape”};
[Link](strings);
[Link](“Sorted Strings:”);
for (String s : strings) {
[Link](s);
}
15) Java Program to Reverse Words in a String:
public class ReverseWords {
public static void main(String[] args) {
String str = “Hello World”;
String[] words = [Link](” “);
StringBuilder reversed = new StringBuilder();
for (int i = [Link] – 1; i >= 0; i–) {
[Link](words[i]).append(” “);
[Link](“Reversed Words: ” + [Link]().trim());
16) Java Program to perform bubble sort on Strings:
import [Link];
public class BubbleSortStrings {
public static void main(String[] args) {
String[] arr = {“banana”, “apple”, “orange”, “grapes”, “pineapple”};
bubbleSort(arr);
[Link](“Sorted Array: ” + [Link](arr));
public static void bubbleSort(String[] arr) {
int n = [Link];
for (int i = 0; i < n – 1; i++) {
for (int j = 0; j < n – i – 1; j++) {
if (arr[j].compareTo(arr[j + 1]) > 0) {
// swap arr[j] and arr[j+1]
String temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
Output:
Sorted Array: [apple, banana, grapes, orange, pineapple]
17) Java program to find occurrence of a character in a String:
public class CharacterOccurrences {
public static void main(String[] args) {
String str = “hello world”;
char ch = „o‟;
int count = 0;
for (int i = 0; i < [Link](); i++) {
if ([Link](i) == ch) {
count++;
[Link](“Occurrences of „” + ch + “„ in the string: ” + count);
}
Output:
Occurrences of „o‟ in the string: 2
18) Java program to count vowels and consonants in a String:
public class VowelsConsonantsCount {
public static void main(String[] args) {
String str = “hello world”;
int vowels = 0, consonants = 0;
str = [Link]();
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if (ch == „a‟ || ch == „e‟ || ch == „i‟ || ch == „o‟ || ch == „u‟) {
vowels++;
} else if (ch >= „a‟ && ch <= „z‟) {
consonants++;
[Link](“Vowels: ” + vowels);
[Link](“Consonants: ” + consonants);
Output:
Vowels: 3
Consonants: 7
19) Program to find number of elements in an array:
public class ArrayLength {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
int length = [Link];
[Link](“Number of elements in the array: ” + length);
Output:
Number of elements in the array: 5
20) Java Program to Calculate average of numbers using Array:
public class AverageOfArray {
public static void main(String[] args) {
int[] array = {5, 10, 15, 20, 25};
int sum = 0;
for (int num : array) {
sum += num;
double average = (double) sum / [Link];
[Link](“Average of numbers in the array: ” + average);
Output:
Average of numbers in the array: 15.0
21) Java Program to Add the elements of an Array:
public class ArraySum {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
int sum = 0;
for (int num : array) {
sum += num;
[Link](“Sum of elements in the array: ” + sum);
Output:
Sum of elements in the array: 15
22) Java Program to reverse an array:
import [Link];
public class ReverseArray {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
reverseArray(array);
[Link](“Reversed array: ” + [Link](array));
public static void reverseArray(int[] array) {
int start = 0;
int end = [Link] – 1;
while (start < end) {
int temp = array[start];
array[start] = array[end];
array[end] = temp;
start++;
end–;
Output:
Reversed array: [5, 4, 3, 2, 1]
23) Java Program to sort an array in ascending order:
import [Link];
public class SortArray {
public static void main(String[] args) {
int[] array = {5, 3, 9, 1, 7};
[Link](array);
[Link](“Sorted array in ascending order: ” + [Link](array));
Output:
Sorted array in ascending order: [1, 3, 5, 7, 9]