Lab 2
Lab 2
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 {
startTime = System.nanoTime();
if(number<=100)
{
if (number >= 90)
System.out.println("A");
endTime = System.nanoTime();
elapsedTime = endTime - startTime;
Task4_a:
Answer:
}
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 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.
Task5_a:
Answer:
Elapsed Time=2901
Task5_b:
Answer:
1. It initializes the GCD variable to 1.
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.
Task5_c:
Answer:
package lab2;
/**
* @author User
*/
import java.util.Scanner;
/**
*/
// Create a Scanner
n1=input.nextInt();
n2=input.nextInt();
startTime=System.nanoTime();
// Initial gcd is 1
// Possible gcd
if (n1 % k == 0 && n2 % k == 0)
endTime=System.nanoTime();
elapsedTime=endTime-startTime;
System.out.println("Elapsed Time="+elapsedTime );
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;
startTime=System.nanoTime();
// Initial gcd is 1
// Possible gcd
endTime=System.nanoTime();
elapsedTime=endTime-startTime;
System.out.println("Elapsed Time="+elapsedTime );
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