java lab
java lab
output
Hello, World!
Output
Output
Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5
ComplexNumber(double r, double i) {
this.real = r;
this.imaginary = i;
}
Output
Sum: 4.0 + 6.0i
Explanation:
Two complex numbers are created:
c1 with real part 2.5 and imaginary part 3.5.
c2 with real part 1.5 and imaginary part 2.5.
The add method is called to add c1 and c2.
The sum of the real parts is 2.5 + 1.5 = 4.0.
The sum of the imaginary parts is 3.5 + 2.5 = 6.0.
The result is printed as: Sum: 4.0 + 6.0i.
Output
Simple Interest: 100.0
8. Write a Program to Print the Pascal's Triangle in Java
public class PascalTriangle {
public static void main(String[] args) {
int rows = 6, coef = 1;
for (int i = 0; i < rows; i++) {
for (int space = 1; space < rows - i; ++space) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
if (j == 0 || i == 0)
coef = 1;
else
coef = coef * (i - j + 1) / j;
System.out.printf("%4d", coef);
}
System.out.println();
}
}
}
output
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Output
Fibonacci Series till 10 terms:
0 1 1 2 3 5 8 13 21 34
Sum of Fibonacci Series: 88