10 Practical Java Programs for Beginners with Outputs

21 days ago

10 Practical Java Programs for Beginners with Outputs

Introduction

Java remains one of the most popular programming languages for beginners due to its structured syntax and wide applicability. This collection of 10 practical programs covers fundamental concepts like input/output operations, conditional statements, and basic calculations. Each program includes complete code and sample output to help you understand the implementation.

1. Student Marks Calculator

This program calculates total marks, percentage, and determines pass/fail status based on 5 subject marks.


import java.util.Scanner;

public class StudentMarks {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter marks of 5 subjects (out of 100):");
        
        int total = 0;
        for(int i=1; i<=5; i++) {
            System.out.print("Subject " + i + ": ");
            int marks = sc.nextInt();
            total += marks;
        }
        
        double percentage = (total / 500.0) * 100;
        String result = (percentage >= 40) ? "Pass" : "Fail";
        
        System.out.println("\nTotal Marks: " + total);
        System.out.println("Percentage: " + percentage + "%");
        System.out.println("Result: " + result);
    }
}

Output:

Enter marks of 5 subjects (out of 100):
Subject 1: 85
Subject 2: 76
Subject 3: 92
Subject 4: 88
Subject 5: 79

Total Marks: 420
Percentage: 84.0%
Result: Pass

2. Days to Months Converter

Converts a given number of days into months and remaining days format.


import java.util.Scanner;

public class DaysConverter {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number of days: ");
        int days = sc.nextInt();
        
        int months = days / 30;
        int remainingDays = days % 30;
        
        System.out.println(days + " days = " + months + " months and " + remainingDays + " days");
    }
}

Output:

Enter number of days: 95
95 days = 3 months and 5 days

3. Seconds to Time Format Converter

Converts total seconds into hours, minutes, and seconds format.


import java.util.Scanner;

public class TimeConverter {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter total seconds: ");
        int totalSeconds = sc.nextInt();
        
        int hours = totalSeconds / 3600;
        int remainingSeconds = totalSeconds % 3600;
        int minutes = remainingSeconds / 60;
        int seconds = remainingSeconds % 60;
        
        System.out.println(totalSeconds + " seconds = " + hours + "h " + minutes + "m " + seconds + "s");
    }
}

Output:

Enter total seconds: 4567
4567 seconds = 1h 16m 7s

4. Age Calculator

Calculates age in years, months, and days based on birth date and current date.


import java.util.Scanner;

public class AgeCalculator {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        System.out.println("Enter current date (yyyy mm dd):");
        int currentYear = sc.nextInt();
        int currentMonth = sc.nextInt();
        int currentDay = sc.nextInt();
        
        System.out.println("Enter birth date (yyyy mm dd):");
        int birthYear = sc.nextInt();
        int birthMonth = sc.nextInt();
        int birthDay = sc.nextInt();
        
        int years = currentYear - birthYear;
        int months = currentMonth - birthMonth;
        int days = currentDay - birthDay;
        
        if (days < 0) {
            months--;
            days += 30; // Approximation
        }
        if (months < 0) {
            years--;
            months += 12;
        }
        
        System.out.println("Your age: " + years + " years, " + months + " months, and " + days + " days");
    }
}

Output:

Enter current date (yyyy mm dd):
2023 10 15
Enter birth date (yyyy mm dd):
1995 5 20
Your age: 28 years, 4 months, and 25 days

5. Positive/Negative Number Checker

Determines whether an entered number is positive, negative, or zero.


import java.util.Scanner;

public class NumberChecker {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        double num = sc.nextDouble();
        
        if (num > 0) {
            System.out.println(num + " is positive");
        } else if (num < 0) {
            System.out.println(num + " is negative");
        } else {
            System.out.println("The number is zero");
        }
    }
}

Output:

Enter a number: -7.5
-7.5 is negative

6. Even or Odd Number Checker

Checks if an entered number is even or odd.


import java.util.Scanner;

public class EvenOddChecker {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = sc.nextInt();
        
        if (num % 2 == 0) {
            System.out.println(num + " is even");
        } else {
            System.out.println(num + " is odd");
        }
    }
}

Output:

Enter a number: 24
24 is even

7. Profit or Loss Calculator

Calculates and displays profit or loss based on cost price and selling price.


import java.util.Scanner;

public class ProfitLossCalculator {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter Cost Price: ");
        double cp = sc.nextDouble();
        System.out.print("Enter Selling Price: ");
        double sp = sc.nextDouble();
        
        if (sp > cp) {
            double profit = sp - cp;
            System.out.println("Profit: " + profit);
        } else if (cp > sp) {
            double loss = cp - sp;
            System.out.println("Loss: " + loss);
        } else {
            System.out.println("No profit, no loss");
        }
    }
}

Output:

Enter Cost Price: 500
Enter Selling Price: 650
Profit: 150.0

8. Smallest of Two Numbers

Finds and displays the smallest of two entered numbers.


import java.util.Scanner;

public class SmallestOfTwo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter first number: ");
        double num1 = sc.nextDouble();
        System.out.print("Enter second number: ");
        double num2 = sc.nextDouble();
        
        double smallest = (num1 < num2) ? num1 : num2;
        System.out.println("Smallest number: " + smallest);
    }
}

Output:

Enter first number: 12.5
Enter second number: 8.3
Smallest number: 8.3

9. Largest of Three Numbers

Determines and displays the largest among three entered numbers.


import java.util.Scanner;

public class LargestOfThree {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter three numbers:");
        double num1 = sc.nextDouble();
        double num2 = sc.nextDouble();
        double num3 = sc.nextDouble();
        
        double largest = num1;
        if (num2 > largest) largest = num2;
        if (num3 > largest) largest = num3;
        
        System.out.println("Largest number: " + largest);
    }
}

Output:

Enter three numbers:
45
78
23
Largest number: 78.0

10. Smallest of Four Numbers

Finds and displays the smallest among four entered numbers.


import java.util.Scanner;

public class SmallestOfFour {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter four numbers:");
        double num1 = sc.nextDouble();
        double num2 = sc.nextDouble();
        double num3 = sc.nextDouble();
        double num4 = sc.nextDouble();
        
        double smallest = Math.min(Math.min(num1, num2), Math.min(num3, num4));
        System.out.println("Smallest number: " + smallest);
    }
}

Output:

Enter four numbers:
12
5
8
3
Smallest number: 3.0

Conclusion

These 10 Java programs cover fundamental programming concepts that form the building blocks for more complex applications. By practicing these examples, beginners can develop a solid understanding of input/output operations, conditional logic, and basic arithmetic operations in Java. Each program demonstrates practical applications of these concepts, making them valuable learning tools for aspiring Java developers.