Sie sind auf Seite 1von 7

University of education vehari campus

Assignment
Submitted by toqeer rao

Roll number bsf1704623

Department information
technology

Semester 4rd

Section morning

Subject data structure


and algorithms

Submitted to sir syed ali


haider Naqvi
1. Create array of size 20 then insert element using
keyboard after that compute and print the sum of all
elements of array.

package sumarray;

import java.util.Scanner;

public class SumArray {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int[] array = new int[20];

int sum = 0;

System.out.println("Enter the elements:");

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

array[i] = input.nextInt();

for( int num : array) {

sum = sum+num;

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

}}

2. Print the occurrence of each element of array.


package countelement;
import java.util.Scanner;

public class CountElement {

public static void main(String[] args) {

int n, x, count = 0, i = 0;

Scanner s = new Scanner(System.in);

System.out.print("Enter no. of elements you want in array:");

n = s.nextInt();

int a[] = new int[n];

System.out.println("Enter all the elements:");

for(i = 0; i < n; i++)

a[i] = s.nextInt();

System.out.print("Enter the element of which you want to count


number of occurrences:");

x = s.nextInt();

for(i = 0; i < n; i++)

if(a[i] == x)

count++;

System.out.println("Number of Occurrence of the


Element:"+count);

}
}

3. Find the even and odd elements of array.


package evenodd2;

import java.util.Scanner;

public class EvenOdd2 {

public static void main(String[] args) {

int n;

Scanner s = new Scanner(System.in);

System.out.print("Enter no. of elements you want in array:");

n = s.nextInt();

int a[] = new int[n];

System.out.println("Enter all the elements:");

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

a[i] = s.nextInt();

System.out.print("Odd numbers:");

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

if(a[i] % 2 != 0)

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

System.out.println();

}}
System.out.print("Even numbers:");

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

if(a[i] % 2 == 0)

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

} } }

4. Find which are the unique elements in array.


package uniqueelement;

import java.util.Scanner;

public class UniqueElement {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

System.out.print("Enter no. of elements you want in array:");

int n = s.nextInt();

int a[] = new int[n];

System.out.println("Enter all the elements:");

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

a[i] = s.nextInt(); }

System.out.print("unique element is:");

int num=a[0];

for(int i=1;i<a.length;i++)
num=num^a[i];

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

}}

5. Find maximum and minimum element in array.


package maxmini;

import java.util.Scanner;

public class Maxmini {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

System.out.print("Enter no. of elements you want in array:");

int n = s.nextInt();

int array[] = new int[n];

System.out.println("Enter all the elements:");

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

array[i] = s.nextInt(); }

System.out.println("max && min values are:");

int max = getMax(array);

System.out.println("Maximum Value is: "+max);

int min = getMin(array);

System.out.println("Minimum Value is: "+min); }

public static int getMax(int[] inputArray){

int maxValue = inputArray[0];

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


if(inputArray[i] > maxValue){

maxValue = inputArray[i]; }

} return maxValue; }

public static int getMin(int[] inputArray){

int minValue = inputArray[0];

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

if(inputArray[i] < minValue){

minValue = inputArray[i]; } }

return minValue;

THANK YOU

Das könnte Ihnen auch gefallen