Lab _2 document
Task1_a:
Answer: There is a limit of printing grade. Because who got grade ‘A’ will
have to get exact 90 marks or who got ‘B’ will have to get exact 80 marks.
This is the logical problem with this program.
Task2_a:
Answer:
if(number>=80 & number<90)
This line has logical problem. We should use here AND operator
Task2_c:
Answer: There is only one logical operator here that is && AND operator
Task2_d:
Answer: There are six conditional operator in this program
Task2_e:
Answer: there is no changes in result after using bitwise operator
Task2_f:
Answer: Here is the modified code where used bitwise operator replaced for logical
operator
package lab2;
import java.util.Scanner;
public class Lab2 {
public static void main(String[] args) {
int number;
long startTime, endTime, elapsedTime;
Scanner input = new Scanner(System.in);
number = input.nextInt();
startTime = System.nanoTime();
if(number<=100)
{
if (number >= 90)
System.out.println("A");
else if (number >= 80 & number < 90)
System.out.println("B");
else if (number >= 70)
System.out.println("C");
else if (number >= 60)
System.out.println("D");
else if (number < 60)
System.out.println("F");
}
else {
System.out.println("Invalid number");
}
endTime = System.nanoTime();
elapsedTime = endTime - startTime;
System.out.println("Elapsed Time=" + elapsedTime);
}
Task4_a:
Answer:
public class Task_4 {
public static void main(String[] args) {
final int NUMBER_OF_TRIALS = 10000000;
int numberOfHits = 0;
for (int i = 0; i < NUMBER_OF_TRIALS; i++)
{
double x = Math.random() * 2.0 - 1;
double y = Math.random() * 2.0 - 1;
if (x * x + y * y <= 1)
{
numberOfHits++;
}
}
double pi = 4.0 * numberOfHits / NUMBER_OF_TRIALS;
System.out.println("Estimated PI is " + pi);
}
}
Here, the purpose of the code segment if (x * x + y * y <= 1) is to check whether a randomly
generated point (x, y) lies within or on the boundary of a unit circle centered at the origin. In
other words, it is checking if the distance from the point (x, y) to the origin (0, 0) is less than or
equal to 1. If the condition is true, it means that the point is inside the circle, and the
“numberOfHits” is incremented. This code segment is used to count the number of points that
fall inside the circle during the Monte Carlo simulation
Task4_b:
Answer:
The logic behind the sentence double pi = 4.0 * numberOfHits / NUMBER_OF_TRIALS; is to
estimate the value of π (pi) using the Monte Carlo method. The formula for this estimation is
derived from the ratio of the number of points that fall inside the unit circle (numberOfHits) to the
total number of random points generated (NUMBER_OF_TRIALS).
The Monte Carlo method leverages the fact that the ratio of the area of the unit circle to the area
of the bounding square is π/4. By multiplying the ratio numberOfHits / NUMBER_OF_TRIALS
by 4.0, you are essentially estimating π. This is because, on average, the ratio will converge to
the actual ratio of the areas, which is π/4, as you generate more and more random points.
So, by calculating double pi = 4.0 * numberOfHits / NUMBER_OF_TRIALS;, you are estimating
the value of π using a probabilistic approach, and the result should be close to the actual value
of π as you increase the number of trials (random points generated).
Task5_a:
Answer:
Elapsed Time=2901
BUILD SUCCESSFUL (total time: 10 seconds)
Task5_b:
Answer:
1. It initializes the GCD variable to 1.
2. It then creates a variable k to store the possible GCD.
3. It starts a while loop that iterates over all possible values of k from 2 to the smaller of n1 and
n2.
4. Inside the while loop, it checks if both n1 and n2 are divisible by k. If they are, then it sets the
GCD variable to k.
5. After the while loop has finished iterating, the GCD variable will contain the greatest common
divisor of n1 and n2.
6. Finally, the program prints the GCD to the console.
Task5_c:
Answer:
Using for loop:
package lab2;
/**
* @author User
*/
import java.util.Scanner;
public class Lab2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Create a Scanner
int gcd=1, n1, n2;
long startTime, endTime,elapsedTime;
Scanner input = new Scanner(System.in);
// Prompt the user to enter two integers
System.out.print("Enter first integer: ");
n1=input.nextInt();
System.out.print("Enter second integer: ");
n2=input.nextInt();
startTime=System.nanoTime();
// Initial gcd is 1
// Possible gcd
for (int k = 2;k <= n1 && k <= n2;k++) {
if (n1 % k == 0 && n2 % k == 0)
gcd = k; // Update gcd
endTime=System.nanoTime();
elapsedTime=endTime-startTime;
System.out.println("Elapsed Time="+elapsedTime );
System.out.println("The greatest common divisor for " + n1 +" and " + n2
+ " is " + gcd);
Task5_d:
answer:
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this
license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package lab2;
/**
*
* @author User
*/
import java.util.Scanner;
public class Lab2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Create a Scanner
int gcd=1, n1, n2;
long startTime, endTime,elapsedTime;
Scanner input = new Scanner(System.in);
// Prompt the user to enter two integers
System.out.print("Enter first integer: ");
n1=input.nextInt();
System.out.print("Enter second integer: ");
n2=input.nextInt();
startTime=System.nanoTime();
// Initial gcd is 1
// Possible gcd
for (int k = 2;k <=n1/2 && k <=n2/2 ;k++) {
if (n1 % k == 0 && n2 % k == 0)
gcd = k; // Update gcd
endTime=System.nanoTime();
elapsedTime=endTime-startTime;
System.out.println("Elapsed Time="+elapsedTime );
System.out.println("The greatest common divisor for " + n1 +" and " + n2
+ " is " + gcd);
}
}
Task5_f:
Answer:
Elapsed Time=300
BUILD SUCCESSFUL (total time: 5 seconds)
Task5_g:
Answer:
Elapsed Time=700
BUILD SUCCESSFUL (total time: 5 seconds)
Task5_h:
Answer:
1. Division Reduction
2. Mathematical Optimizations
3. Theoretical Efficiency
4. Ease of Implementation