0% found this document useful (0 votes)
12 views4 pages

Assignments 01-DS-Ai231032

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

Assignments 01-DS-Ai231032

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

Course Code/ Title: ASSESSMENT ACTIVITY: Semester:

(AI-2001) Data Structures and Algorithms Assignment -01 Semester -III

Final Dated: Nov 28th 2024 OBE Target: Weight of Marks:


CLO-2, GA-3, C-3
Student Name: Moosa Aijaz Teacher: Score:
Student ID: Ai231032 Abdul Salam

Question 1:
Construct a Java Code that finds a given number from an array. The elements in the array

would be entered by the user.

import java.util.Scanner;

public class FindNumber {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

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

int n = scanner.nextInt();

int[] array = new int[n];

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

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

array[i] = scanner.nextInt();

System.out.print("Enter the number to find: ");

int target = scanner.nextInt();


boolean found = false;

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

if (array[i] == target) {

System.out.println("Number " + target + " found at index " + i);

found = true;

break;

if (!found) {

System.out.println("Number " + target + " not found in the array.");

scanner.close();

Question 2:
Construct a Java Code that finds minimum and maximum number from an array and

display numbers and its memory address.

import java.util.Scanner;
public class MinMaxFinder {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

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

int n = scanner.nextInt();

int[] array = new int[n];

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

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

array[i] = scanner.nextInt();

int min = array[0], max = array[0];

int minIndex = 0, maxIndex = 0;

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

if (array[i] < min) {

min = array[i];

minIndex = i;

if (array[i] > max) {

max = array[i];

maxIndex = i;

}
}

System.out.println("Minimum number: " + min + " at memory address (index):


" + minIndex);

System.out.println("Maximum number: " + max + " at memory address


(index): " + maxIndex);

scanner.close();

You might also like