You are on page 1of 8

Lab Rport 3

Hassan Tahamid
ID:0242220005101607
Section:N
• You have an Array of N numbers. Now write a code to Sort the
elements in Ascending Order.

import java.util.Arrays;
import java.util.Scanner;

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

System.out.print("How many numbers do you want to insert? ");


int N = scanner.nextInt();

int[] numbers = new int[N];

System.out.println("Enter " + N + " array elements:");

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


numbers[i] = scanner.nextInt();
}

Arrays.sort(numbers);

System.out.println("Sorted array in ascending order:");


for (int number : numbers) {
System.out.print(number + " ");
}
scanner.close();
}
}

• Suppose you have stored the CGPA of N number of students


in an array. Now find the smallest and largest CGPA of the
array.

import java.util.Scanner;

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

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


int N = scanner.nextInt();
double[] cgpaArray = new double[N];
System.out.println("Enter the CGPA values for " + N + " students:");

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


System.out.print("Enter CGPA for student " + (i + 1) + ": ");
cgpaArray[i] = scanner.nextDouble();

double smallestCGPA = cgpaArray[0];


double largestCGPA = cgpaArray[0];

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


if (cgpaArray[i] < smallestCGPA) {
smallestCGPA = cgpaArray[i];
}
if (cgpaArray[i] > largestCGPA) {
largestCGPA = cgpaArray[i];
}
}

System.out.println("Smallest CGPA: " + smallestCGPA);


System.out.println("Largest CGPA: " + largestCGPA);

scanner.close();
}
}
• Find absolute, floor, ceil, round and square root values of a
number.
import java.util.Scanner;
import java.lang.Math;

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

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


double number = scanner.nextDouble();

double absoluteValue = Math.abs(number);


System.out.println("Absolute Value: " + absoluteValue);

double floorValue = Math.floor(number);


System.out.println("Floor Value: " + floorValue);

double ceilValue = Math.ceil(number);


System.out.println("Ceiling Value: " + ceilValue);

double roundValue = Math.round(number);


System.out.println("Round Value: " + roundValue);

double squareRoot = Math.sqrt(absoluteValue);


System.out.println("Square Root: " + squareRoot);

scanner.close();
}
}

• Find the maximum and minimum values from three numbers


using MATH Class.
import java.util.Scanner;
import java.lang.Math;

import java.util.Scanner;

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

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


double num1 = scanner.nextDouble();

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


double num2 = scanner.nextDouble();

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


double num3 = scanner.nextDouble();

double max = Math.max(Math.max(num1, num2), num3);


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

double min = Math.min(Math.min(num1, num2), num3);


System.out.println("Minimum value: " + min);

scanner.close();

}
}
• Calculate the area of a circle using Math.pow() and Math.PI
methods
import java.util.Scanner;

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

System.out.print("Enter the radius of the circle: ");


double radius = scanner.nextDouble();

double area = Math.PI * Math.pow(radius, 2);

System.out.println("The area of the circle is: " + area);

scanner.close();
}
}

You might also like