JAVA

Java Array Programs


1. Print Array Elements

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter size of array: ");
        int size = sc.nextInt();

        int[] arr = new int[size];

        System.out.print("Enter array elements: ");
        for (int i = 0; i < size; i++) {
            arr[i] = sc.nextInt();
        }

        System.out.println("Array Elements:");
        for (int i = 0; i < size; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}

2. Sum of Array Elements

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter size of array: ");
        int size = sc.nextInt();

        int[] arr = new int[size];

        System.out.print("Enter array elements: ");
        for (int i = 0; i < size; i++) {
            arr[i] = sc.nextInt();
        }

        int sum = 0;

        for (int i = 0; i < size; i++) {
            sum += arr[i];
        }

        System.out.println("Sum of Array Elements: " + sum);
    }
}

3. Average of Array Elements

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter size of array: ");
        int size = sc.nextInt();

        int[] arr = new int[size];

        System.out.print("Enter array elements: ");
        for (int i = 0; i < size; i++) {
            arr[i] = sc.nextInt();
        }

        int sum = 0;

        for (int i = 0; i < size; i++) {
            sum += arr[i];
        }

        int average = sum / size;

        System.out.println("Average of Array Elements: " + average);
    }
}

4. Find Maximum Element in Array

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter size of array: ");
        int size = sc.nextInt();

        int[] arr = new int[size];

        System.out.print("Enter array elements: ");
        for (int i = 0; i < size; i++) {
            arr[i] = sc.nextInt();
        }

        int max = 0;

        for (int i = 1; i < size; i++) {
            if (arr[i] > arr[max]) {
                max = i;
            }
        }

        System.out.println("Maximum Element: " + arr[max]);
    }
}

5. Find Minimum Element in Array

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter size of array: ");
        int size = sc.nextInt();

        int[] arr = new int[size];

        System.out.print("Enter array elements: ");
        for (int i = 0; i < size; i++) {
            arr[i] = sc.nextInt();
        }

        int min = 0;

        for (int i = 1; i < size; i++) {
            if (arr[i] < arr[min]) {
                min = i;
            }
        }

        System.out.println("Minimum Element: " + arr[min]);
    }
}

6. Count Even and Odd Numbers in Array

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter size of array: ");
        int size = sc.nextInt();

        int[] arr = new int[size];

        System.out.print("Enter array elements: ");
        for (int i = 0; i < size; i++) {
            arr[i] = sc.nextInt();
        }

        int evenCount = 0;
        int oddCount = 0;

        for (int i = 0; i < size; i++) {

            if (arr[i] % 2 == 0) {
                evenCount++;
            } else {
                oddCount++;
            }
        }

        System.out.println("Even Numbers Count: " + evenCount);
        System.out.println("Odd Numbers Count: " + oddCount);
    }
}

7. Print Array in Reverse Order

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter size of array: ");
        int size = sc.nextInt();

        int[] arr = new int[size];

        System.out.print("Enter array elements: ");
        for (int i = 0; i < size; i++) {
            arr[i] = sc.nextInt();
        }

        System.out.println("Array in Reverse Order:");

        for (int i = arr.length - 1; i >= 0; i--) {
            System.out.print(arr[i] + " ");
        }
    }
}

 class Node:

    def __init__(self,data):

        self.data=data

        self.next=None

class ll:

    def __init__(self):

        self.head=None

    def Insert(self,data):

        newnode=Node(data)

        if self.head is None:

            self.head=newnode

            return

        temp=self.head

        while temp.next is not None:

            temp=temp.next

        temp.next = newnode


    def Display(self):

        temp = self.head

        while temp is not None:

            print(temp.data, end=" -> ")

            temp = temp.next

        print("None")

    def mid(self):

        slo=self.head

        fas=self.head

        while fas is not None  and fas.next is not None :

             slo=slo.next

             fas=fas.next.next

        return slo.data

        

    def delete(self,val):

        temp=self.head

        if temp is None:

            return

        if temp.data==val:

            self.head=temp.next

            return

        while(temp.next is not None):

            if(temp.next.data==val):

                temp.next=temp.next.next

                return

            temp=temp.next

    def rev(self):

        prev=None

        curr=self.head

        

        while curr:

            nextnode=curr.next

            curr.next=prev

            prev=curr

            curr=nextnode

        self.head=prev

        

l = ll()


l.Insert(10)

l.Insert(20)

l.Insert(30)


l.Display()

print("Middle:", l.mid())

l.delete(30)

l.Display()

l.rev()

l.Display()

Comments

Popular posts from this blog

COCO FARMING MACHINE