1) Hello World Program:
public class HelloWorld {
public static void main(String[] args) {
[Link](“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);
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);
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.”);
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);
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]();
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);
10) 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.
11) 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.”);
12) Calculate Compound Interest in Java:
public class CompoundInterest {
public static void main(String[] args) {
double principal = 15000, rate = 5.5, time = 3;
double compoundInterest = principal * ([Link]((1 + rate / 100), time)) – principal;
[Link](“Compound Interest: ” + compoundInterest);