You are on page 1of 5

Name : Jeel Mehta

Reg. No. : 20BCE1117


Sub : Java Programming

Lab - 2(Array)
Q2

import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int a[];
        a = new int[10];
        int b[];
        b= new int[10];
        System.out.println("\n Enter the no. of elements in the array");
        int n;
        n = sc.nextInt();
        int i;
        for(i=0;i<n;i++)
        {
            System.out.println("\n Enter the "+i +" element of the array:");
            a[i] = sc.nextInt();
        }
        System.out.println("\n Copied array is :");
        for(i=0;i<n;i++)
        {
           
                b[i]=a[i];
                System.out.println("\n" + b[i]);
           
        }
       
    }
}
Q3

import java.util.*;
public class q3
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int n;
        int count=0;
        int flag =0;
        int a[];
        a = new int[10];
        System.out.println("\n Enter the no. of elements :");
        n = sc.nextInt();
        int i;
        for(i=0;i<n;i++)
        {
            System.out.println("\n Enter the "+i+"th element:");
            a[i]=sc.nextInt();
        }
        for(i=0;i<n;i++)
        {
            if(a[i]%2==0)
            {
                count++;
            }
            else
            {
                flag++;
            }
        }
        System.out.println("\n No. of Odd elements: "+flag);
        System.out.println("\n No. of Even elements: "+count);
    }
}
Q5

import java.util.*;
public class q5
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int a[];
        double avg=0,sum=0;
        a =new int [10];
        int i,n;
        System.out.println("\n Enter the no. of elements :");
        n = sc.nextInt();
        for(i=0;i<n;i++)
        {
            System.out.println("\nEnter the "+ i+"th element:");
            a[i]=sc.nextInt();
        }
        for(i=1;i<n-1;i++)
        {
            sum = sum + a[i];
        }
        avg = sum/n;
        System.out.println("\n Average of the array is :" + avg);

    }
}
\

You might also like