0% found this document useful (0 votes)
94 views6 pages

Lab 1.1

This document is a lab manual for a data structures and algorithms course in the fall of 2019. It contains solutions to 4 questions asked as part of Lab 1. The questions involve tasks such as swapping elements between two arrays, dividing elements of one array by another, calculating the factorial of a number, and finding the maximum number in an array. The document provides the name of the instructor, student, student ID number, department, and lab number for identification purposes.

Uploaded by

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

Lab 1.1

This document is a lab manual for a data structures and algorithms course in the fall of 2019. It contains solutions to 4 questions asked as part of Lab 1. The questions involve tasks such as swapping elements between two arrays, dividing elements of one array by another, calculating the factorial of a number, and finding the maximum number in an array. The document provides the name of the instructor, student, student ID number, department, and lab number for identification purposes.

Uploaded by

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

Lab Manual

Data Structure and Algorithm


Fall 2019

Instructor Mr. Atif

Student Name Muhammad Athar

CMSID 45756

Department Software engineering

Lab Lab 1
Question Number 1:

Solution:

import java.util.Scanner;
public class q1
{
public static void main(String[] args)
{
int[] array1 = new int[]{ 1,2,3,4 };
int[] array2 = new int[]{ 5,6,7,8 };

//******************Output*********************
System.out.println("Before Swapping");
System.out.println("First Array ");
for (int i = 0; i<array1.length; i++)
{
System.out.print(array1[i]);
}
System.out.println("\nSecond Array ");
for (int i=0; i<array2.length; i++)
{
System.out.print(array2[i]);
}

//******************Swapping*********************
for (int i = 0; i<array1.length && i<array2.length; i++)
{
int[] temp = new int[] {0,0,0,0};
temp[i] = array1[i];
array1[i] = array2[i];
array2[i] = temp[i];
}
//******************Output*********************
System.out.println("\nArrays after Swapping");
System.out.println("First Array ");
for (int i=0; i<array2.length; i++)
{
System.out.print(array1[i]);
}
System.out.println("\nSecond Array ");
for (int i=0; i<array2.length; i++)
{
System.out.print(array2[i]);
}

}
}
Question Number 2:

Solution:
public class q2
{
public static void main(String[] args)
{
int[] array1 = new int[]{ 1,2,3,4 };
int[] array2 = new int[]{ 5,7,7,8 };
int[] p= new int[] {0,0,0,0};
//******************Output*********************

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


for (int i = 0; i<array1.length; i++)
{
System.out.print(array1[i]);
}
System.out.println("\nSecond Array ");
for (int i=0; i<array2.length; i++)
{
System.out.print(array2[i]);
}

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


{

p[i]=array2[i]/array1[i];

//******************Output*********************
System.out.println("\n");
System.out.println("After Division ");
for (int i=0; i<p.length; i++)
{
System.out.print(""+p[i]);
}

}
}

Question Number 3;

Solution:

class Test
{
static int factorial(int n)
{
int res = 1, i;
for (i=2; i<=n; i++)
res *= i;
return res;
}

public static void main(String[] args)


{
int num = 5;
System.out.println("Factorial of "+ num + " is " + factorial(5));
}
}

Question Number 4:

Solution:

public class q4 {

public static void main(String[] args)


{
int[] array1 = new int[] {21,22,23,45};
int max=0;
for (int i=0;i<array1.length;i++)
{
if(max<array1[i])
{
max=array1[i];
}
}
System.out.println("max number in array is : "+max);
}
}

You might also like