0% found this document useful (0 votes)
15 views

Assignment 7

Uploaded by

megh94001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Assignment 7

Uploaded by

megh94001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Name-Megh Patel

sub-SPM
Div-B

Assignment-7
import java.util.Scanner;
public class arry1
{
public static void main(String args[])
{
//search an element
Scanner sc=new Scanner(System.in);
System.out.println("enter the array length : ");
int n=sc.nextInt();

int number[]=new int[n];

System.out.println("enter the array elements :");


for(int i=0;i<number.length;i++)
number[i]=sc.nextInt();

System.out.println("Array elements ");


for(int i=0;i<number.length;i++)
System.out.println(number[i]);

//check for a duplicate value


System.out.println("duplicate element");
for(int i=0;i<number.length;i++)
{
for(int j=0;j<number.length;j++)

if(number[i]==number[j]&&i!=j)

System.out.println(number[i]);
}
//sum and product
int sum = 0;
int product = 1;

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


sum += number[i];
product *= number[i];
}

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

System.out.println("Product of the elements: " + product);

}
}

run:
Name-Megh Patel
sub-SPM
Div-B

enter the array length :


3
enter the array elements :
10
10
20
Array elements
10
10
20
duplicate element
10
Sum of the elements: 40
Product of the elements: 2000

Q-2)
import java.util.Scanner;

public class arry2


{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);

String[] array1 = new String[3];


String[] array2 = new String[3];
String[] commonElements = new String[3];

System.out.print("Enter the elements of the first array: ");


for (int i = 0; i < array1.length; i++)
{
array1[i] = sc.nextLine();
}

System.out.print("Enter the elements of the second array: ");


for (int i = 0; i < array2.length; i++) {
array2[i] = sc.nextLine();
}

int commonIndex = 0;

for (int i = 0; i < 3; i++)


{
for (int j = 0; j < 3; j++)
{
if (array1[i].equals(array2[j]))
Name-Megh Patel
sub-SPM
Div-B

{
commonElements[commonIndex] = array1[i];
commonIndex++;
}
}
}

System.out.println(commonElements[2]);
}
}

You might also like