List of Simple Java Programs Pdf


Introduction

Welcome to the exciting world of Java programming! Artwork – multiple works of the same theme in various styles, techniques, or directions. Regardless of your level of expertise with coding in Java, this guide aims to walk you through simple examples and comprehend the fundamentals of the language. You will see the following 20 examples of programs written in Java for beginners that introduce basic notions including syntax, structures with seven subdivisions, loops, methods, and so forth. Let’s get right into it and begin programming!

List of Simple Java Programs Pdf


1. Hello World Program

Description: The “Hello World” program is the classic starter code for any beginner. It teaches you how to output text to the console.

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

2. Input from User

Description: Learn to take user input using the Scanner class.

import java.util.Scanner;

public class UserInput {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter your name: ");
        String name = scanner.nextLine();
        System.out.println("Hello, " + name + "!");
        scanner.close();
    }
}

3. Add Two Numbers

Description: A simple program to add two numbers input by the user.

import java.util.Scanner;

public class AddNumbers {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter first number: ");
        int first = scanner.nextInt();
        System.out.print("Enter second number: ");
        int second = scanner.nextInt();
        int sum = first + second;
        System.out.println("The sum is: " + sum);
        scanner.close();
    }
}

4. Calculate Area of a Circle

Description: This program calculates the area of a circle given the radius.

import java.util.Scanner;

public class CircleArea {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the radius of the circle: ");
        double radius = scanner.nextDouble();
        double area = Math.PI * radius * radius;
        System.out.println("Area of the circle is: " + area);
        scanner.close();
    }
}

5. Check Even or Odd

Description: Check whether a given number is even or odd.

import java.util.Scanner;

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

6. Factorial of a Number

Description: Calculate the factorial of a number using a loop.

import java.util.Scanner;

public class Factorial {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int n = scanner.nextInt();
        int factorial = 1;
        for (int i = 1; i <= n; i++) {
            factorial *= i;
        }
        System.out.println("Factorial of " + n + " is " + factorial);
        scanner.close();
    }
}

7. Generate Multiplication Table

Description: Generate and display the multiplication table of a given number.

import java.util.Scanner;

public class MultiplicationTable {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int number = scanner.nextInt();
        for (int i = 1; i <= 10; i++) {
            System.out.println(number + " * " + i + " = " + (number * i));
        }
        scanner.close();
    }
}

8. Reverse a String

Description: A program to reverse a string entered by the user.

import java.util.Scanner;

public class ReverseString {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String input = scanner.nextLine();
        String reversed = new StringBuilder(input).reverse().toString();
        System.out.println("Reversed string: " + reversed);
        scanner.close();
    }
}

9. Check Palindrome

Description: Check if the given string is a palindrome.

import java.util.Scanner;

public class Palindrome {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String str = scanner.nextLine();
        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.");
        }
        scanner.close();
    }
}

10. Fibonacci Series

Description: Display the Fibonacci series up to a certain number.

import java.util.Scanner;

public class Fibonacci {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the number of terms: ");
        int count = scanner.nextInt();
        int n1 = 0, n2 = 1;
        System.out.print("First " + count + " terms of Fibonacci series: ");

        for (int i = 1; i <= count; ++i) {
            System.out.print(n1 + " ");
            int sum = n1 + n2;
            n1 = n2;
            n2 = sum;
        }
        scanner.close();
    }
}

11. Find Largest Among Three Numbers

Description: Determine the largest number among three numbers entered by the user.

import java.util.Scanner;

public class LargestNumber {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter first number: ");
        double first = scanner.nextDouble();
        System.out.print("Enter second number: ");
        double second = scanner.nextDouble();
        System.out.print("Enter third number: ");
        double third = scanner.nextDouble();

        double largest = first;

        if (second > largest) {
            largest = second;
        }
        if (third > largest) {
            largest = third;
        }

        System.out.println("The largest number is " + largest);
        scanner.close();
    }
}

12. Switch Case Example

Description: A simple example of using switch-case to handle multiple conditions.

import java.util.Scanner;

public class SwitchExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number between 1 and 4: ");
        int choice = scanner.nextInt();

        switch (choice) {
            case 1:
                System.out.println("You selected option 1.");
                break;
            case 2:
                System.out.println("You selected option 2.");
                break;
            case 3:
                System.out.println("You selected option 3.");
                break;
            case 4:
                System.out.println("You selected option 4.");
                break;
            default:
                System.out.println("Invalid choice.");
        }
        scanner.close();
    }
}

13. Simple Interest Calculator

Description: Calculate the simple interest based on principal, rate, and time.

import java.util.Scanner;

public class SimpleInterest {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the principal: ");
        double principal = scanner.nextDouble();
        System.out.print("Enter the rate of interest per annum: ");
        double rate = scanner.nextDouble();
        System.out.print("Enter the time in years: ");
        int time = scanner.nextInt();

        double interest = (principal * rate * time) / 100;
        System.out.println("Simple Interest is: " + interest);
        scanner.close();
    }
}

14. Convert Temperature from Fahrenheit to Celsius

Description: Convert a temperature given in Fahrenheit to Celsius.

import java.util.Scanner;

public class TemperatureConversion {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter temperature in Fahrenheit: ");
        double fahrenheit = scanner.nextDouble();
        double celsius = (fahrenheit - 32) * 5 / 9;
        System.out.println("Temperature in Celsius is: " + celsius);
        scanner.close();
    }
}

15. Check Prime Number

Description: Determine if a number is prime or not.

import java.util.Scanner;

public class PrimeCheck {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = scanner.nextInt();
        boolean flag = false;
        for (int i = 2; i <= num / 2; ++i) {
            if (num % i == 0) {
                flag

 = true;
                break;
            }
        }

        if (!flag)
            System.out.println(num + " is a prime number.");
        else
            System.out.println(num + " is not a prime number.");
        scanner.close();
    }
}

16. Count Number of Digits in an Integer

Description: This program counts the number of digits in a given integer. It’s a great way to understand loops and conditions in Java.

import java.util.Scanner;

public class CountDigits {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        int number = scanner.nextInt();
        int count = 0;
        while (number != 0) {
            number /= 10;  // Divides the number by 10 to strip off the last digit
            ++count;       // Increment the count for each digit stripped
        }
        System.out.println("Number of digits: " + count);
        scanner.close();
    }
}

This program uses a while loop to repeatedly divide the input number by 10, stripping off the last digit each time until the number reduces to zero. It counts how many times this loop runs, which corresponds to the number of digits in the original number.

17. Count Number of Digits in an Integer

Description: Count how many digits are in an integer.

import java.util.Scanner;

public class CountDigits {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        int number = scanner.nextInt();
        int count = 0;
        while (number != 0) {
            number /= 10;  // remove the last digit of the number
            ++count;
        }
        System.out.println("Number of digits: " + count);
        scanner.close();
    }
}

18. Calculate Power of a Number

Description: Program to calculate the power of a number using a non-negative exponent.

import java.util.Scanner;

public class PowerCalculator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the base: ");
        double base = scanner.nextDouble();
        System.out.print("Enter the exponent: ");
        int exponent = scanner.nextInt();
        double result = Math.pow(base, exponent);
        System.out.println(base + " raised to the power " + exponent + " is " + result);
        scanner.close();
    }
}

19. Binary to Decimal Converter

Description: Convert a binary number (entered as a string) to its decimal equivalent.

import java.util.Scanner;

public class BinaryToDecimal {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a binary number: ");
        String binaryString = scanner.nextLine();
        int decimal = Integer.parseInt(binaryString, 2);
        System.out.println("Decimal equivalent is: " + decimal);
        scanner.close();
    }
}

20. Array Sum

Description: Calculate the sum of elements in an array.

public class ArraySum {
    public static void main(String[] args) {
        int[] array = {10, 20, 30, 40, 50};
        int sum = 0;
        for (int num : array) {
            sum += num;
        }
        System.out.println("Sum of array elements is: " + sum);
    }
}

Conclusion

Well done on finishing these 20 easy to do List of Simple Java Programs Pdf! Not only did you finish the whole set, but also accomplished particular goals which prepared you to go further. Do not stop practicing these examples and more so, feel free to change and play around in order to improve your California understanding of Java programming. Enjoy!

Call to Action

Should you wish or be able, we hope that you found this post useful and you would like to share it with some friends that are practicing Java,.. Also, would be interested to know what was the umm best program in your opinion? or what other subjects would you like us to write in the next publication!


This type of presentation will stand most probably work for the beginners, as well, however, encourage them improve their skills in Java programming.

Pdf of Java Programs

1 thought on “List of Simple Java Programs Pdf”

Leave a Comment