Wednesday, 12 April 2023

Beginner Level Java programs

 



Array Examples:


1. Program to find the sum of all elements in an array:


public class ArraySum {

    public static void main(String[] args) {

        int[] arr = {2, 4, 6, 8, 10};

        int sum = 0;


        for (int i = 0; i < arr.length; i++) {

            sum += arr[i];

        }


        System.out.println("Sum of elements in array: " + sum);

    }

}



2. Program to find the maximum element in an array:


public class ArrayMax {

    public static void main(String[] args) {

        int[] arr = {2, 4, 6, 8, 10};

        int max = arr[0];


        for (int i = 1; i < arr.length; i++) {

            if (arr[i] > max) {

                max = arr[i];

            }

        }


        System.out.println("Maximum element in array: " + max);

    }

}









3. Program to find the minimum element in an array:


public class ArrayMin {

    public static void main(String[] args) {

        int[] arr = {2, 4, 6, 8, 10};

        int min = arr[0];


        for (int i = 1; i < arr.length; i++) {

            if (arr[i] < min) {

                min = arr[i];

            }

        }


        System.out.println("Minimum element in array: " + min);

    }

}


4. Program to search for an element in an array:


public class ArraySearch {

    public static void main(String[] args) {

        int[] arr = {2, 4, 6, 8, 10};

        int num = 8;

        boolean found = false;


        for (int i = 0; i < arr.length; i++) {

            if (arr[i] == num) {

                found = true;

                break;

            }

        }


        if (found) {

            System.out.println(num + " found in array");

        } else {

            System.out.println(num + " not found in array");

        }

    }

}







5. Program to sort an array in ascending order:


public class ArraySort {

    public static void main(String[] args) {

        int[] arr = {10, 2, 8, 6, 4};

        int temp;


        for (int i = 0; i < arr.length; i++) {

            for (int j = i + 1; j < arr.length; j++) {

                if (arr[i] > arr[j]) {

                    temp = arr[i];

                    arr[i] = arr[j];

                    arr[j] = temp;

                }

            }

        }


        System.out.println("Sorted array in ascending order:");

        for (int i = 0; i < arr.length; i++) {

            System.out.print(arr[i] + " ");

        }

    }

}




For loop-based programs


6. Program to print the numbers from 1 to 10:


public class PrintNumbers {

    public static void main(String[] args) {

        for (int i = 1; i <= 10; i++) {

            System.out.print(i + " ");

        }

    }

}


7. Program to print the even numbers from 1 to 20:


public class PrintEvenNumbers {

    public static void main(String[] args) {

        for (int i = 2; i <= 20; i += 2) {

            System.out.print(i + " ");

        }

    }

}



8. Program to print the sum of the numbers from 1 to 100:


public class SumNumbers {

    public static void main(String[] args) {

        int sum = 0;


        for (int i = 1; i <= 100; i++) {

            sum += i;

        }


        System.out.println("Sum of numbers from 1 to 100: " + sum);

    }

}



8. Program to print the multiplication table of a number:


import java.util.Scanner;


public class MultiplicationTable {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.print("Enter a number: ");

        int num = input.nextInt();


        for (int i = 1; i <= 10; i++) {

            System.out.println(num + " x " + i + " = " + (num * i));

        }

    }

}




9. Program to print a right-angled triangle using asterisks:


import java.util.Scanner;


public class RightTriangle {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.print("Enter the height of the triangle: ");

        int height = input.nextInt();


        for (int i = 1; i <= height; i++) {

            for (int j = 1; j <= i; j++) {

                System.out.print("*");

            }

            System.out.println();

        }

    }

}


While loop-based programs


10. Program to print the numbers from 1 to 10:


public class PrintNumbers {

    public static void main(String[] args) {

        int i = 1;


        while (i <= 10) {

            System.out.print(i + " ");

            i++;

        }

    }

}











11. Program to print the sum of the numbers from 1 to 100:


public class SumNumbers {

    public static void main(String[] args) {

        int sum = 0;

        int i = 1;


        while (i <= 100) {

            sum += i;

            i++;

        }


        System.out.println("Sum of numbers from 1 to 100: " + sum);

    }

}


12. Program to find the factorial of a number:


import java.util.Scanner;


public class Factorial {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.print("Enter a number: ");

        int num = input.nextInt();

        int factorial = 1;

        int i = 1;


        while (i <= num) {

            factorial *= i;

            i++;

        }


        System.out.println("Factorial of " + num + " = " + factorial);

    }

}







Do While loop-based programs


13. Program to print the sum of the numbers from 1 to 100:


 public class SumNumbers {

    public static void main(String[] args) {

        int sum = 0;

        int i = 1;


        do {

            sum += i;

            i++;

        } while (i <= 100);


        System.out.println("Sum of numbers from 1 to 100: " + sum);

    }

}



14. Program to ask the user for a password until it matches a predetermined password:


import java.util.Scanner;


public class Password {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        String password = "password123";

        String userPassword;


        do {

            System.out.print("Enter the password: ");

            userPassword = input.nextLine();

        } while (!userPassword.equals(password));


        System.out.println("Password accepted!");

    }

}






Class and object-based programs


15. Program to create a simple class and object:


public class MyClass {

    int myVariable;


    public static void main(String[] args) {

        MyClass obj = new MyClass();

        obj.myVariable = 10;

        System.out.println(obj.myVariable);

    }

}


16. Program to create a class with a constructor


public class Person {

    String name;

    int age;


    public Person(String name, int age) {

        this.name = name;

        this.age = age;

    }


    public static void main(String[] args) {

        Person person = new Person("John", 30);

        System.out.println("Name: " + person.name);

        System.out.println("Age: " + person.age);

    }

}


17. Program to create a class with methods


public class Rectangle {

    int length;

    int width;


    public Rectangle(int length, int width) {

        this.length = length;

        this.width = width;

    }


    public int area() {

        return length * width;

    }


    public int perimeter() {

        return 2 * (length + width);

    }


    public static void main(String[] args) {

        Rectangle rect = new Rectangle(5, 7);

        System.out.println("Area: " + rect.area());

        System.out.println("Perimeter: " + rect.perimeter());

    }

}


No comments :

Post a Comment