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

22-SE-096 Lab Report 7

Uploaded by

Talha Ghafoor
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)
21 views

22-SE-096 Lab Report 7

Uploaded by

Talha Ghafoor
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/ 9

HITEC UNIVERSITY TAXILA

Submitted to: Mr. Talha Saeed


Lab Report # 03 Submitted
by: Talha Ghafoor
Registration Number: 22-SE- 096
Section: 2A
Course Name: Object Oriented Programming (OOP) Course Code:
CS-104

DEPARTMENT OF SOFTWARE ENGINEERING HITEC UNIVERSITY, TAXILA


Lab Report # 7

JAVA PROGRAMMING LANGUAGE

Program Number 01:


Objective:

import java.util.Scanner;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of subjects: ");


int numSubjects = scanner.nextInt();

int[] marks = new int[numSubjects];

// Input marks for each subject


for (int i = 0; i < numSubjects; i++) {
System.out.print("Enter the marks for subject " + (i + 1) + ": ");
marks[i] = scanner.nextInt();
}

// Calculate the sum of marks


int sum = 0;
for (int i = 0; i < numSubjects; i++) {
sum += marks[i];
}

// Calculate the average

DEPARTMENT OF SOFTWARE ENGINEERING HITEC UNIVERSITY, TAXILA


double average = (double) sum / numSubjects;

// Calculate the percentage


double percentage = (average / 100) * 100;

// Output the average and percentage


System.out.println("Average marks: " + average);
System.out.println("Percentage: " + percentage + "%");

scanner.close();

// TODO code application logic here


}
}

Output:

Program Number 02:


Objective:
Write a program that reverse the elements of an array so that the last one become the first
one and the second one become the second last one. Let there be five elements in the array

public static void main(String[] args) {


int[] array = {1, 2, 3, 4, 5};

DEPARTMENT OF SOFTWARE ENGINEERING HITEC UNIVERSITY, TAXILA


System.out.println("Original Array:");
printArray(array);

reverseArray(array);

System.out.println("\nReversed Array:");
printArray(array);
}

public static void reverseArray(int[] array) {


int start = 0;
int end = array.length - 1;

while (start < end) {


// Swap elements at start and end indices
int temp = array[start]; array[start] =
array[end];
array[end] = temp;

// Move start index towards the end


start++;

// Move end index towards the start


end--;
}
}

public static void printArray(int[] array) {


for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}

// TODO code application logic here


}

}
Output:

DEPARTMENT OF SOFTWARE ENGINEERING HITEC UNIVERSITY, TAXILA


Program Number 03:
Objective:
We have two arrays A and B, each of 10 integers. Write a Program that tests if every
element of array A is equal to its corresponding element in array B. In other words, the
program must check if A[0] is equal to B[0], A[1] is equal to B[1] and so forth. The program is
to return true (1) if all elements are equal and false if at least one element is not equal.

DEPARTMENT OF SOFTWARE ENGINEERING HITEC UNIVERSITY, TAXILA


public static void main(String[] args) {
int[] arrayA = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] arrayB = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

boolean result = checkArrayEquality(arrayA, arrayB);

System.out.println("Arrays are equal: " + result);


}

public static boolean checkArrayEquality(int[] arrayA, int[] arrayB)


{
if (arrayA.length != arrayB.length) {
return false;
}

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


if (arrayA[i] != arrayB[i]) {
return false;
}
}

return true;
// TODO code application logic here
}

Output:

DEPARTMENT OF SOFTWARE ENGINEERING HITEC UNIVERSITY, TAXILA


Program Number 04:
Objective:
Write a program that checks whether any of digits in a number appear more than once. After
the user enters a number, the program prints either repeated digit or no repeated digit:
Enter a Number: 1233422Repeated Digit 3, 2

DEPARTMENT OF SOFTWARE ENGINEERING HITEC UNIVERSITY, TAXILA


import java.util.HashSet; import
java.util.Scanner;
import java.util.Set;

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a Number: ");


int number = scanner.nextInt();

boolean hasRepeatedDigit = checkRepeatedDigit(number);

if (hasRepeatedDigit) {
System.out.println("Repeated Digit(s) found");
} else {
System.out.println("No Repeated Digit found");
}

scanner.close();
}

public static boolean checkRepeatedDigit(int number) {


Set<Integer> digitSet = new HashSet<>();

while (number > 0) {


int digit = number % 10;

if (digitSet.contains(digit)) {
return true;
}

digitSet.add(digit);
number /= 10;
}

return false;
// TODO code application logic here
}

Output:

DEPARTMENT OF SOFTWARE ENGINEERING HITEC UNIVERSITY, TAXILA


DEPARTMENT OF SOFTWARE ENGINEERING HITEC UNIVERSITY, TAXILA

You might also like