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

java lab

The document contains Java programming examples for various tasks, including printing 'Hello World', adding two numbers, swapping numbers, converting between integers and binary, calculating factorials, adding complex numbers, calculating simple interest, printing Pascal's Triangle, and finding the sum of Fibonacci series. Each example includes the Java code and the corresponding output. These examples serve as practical demonstrations of basic programming concepts in Java.

Uploaded by

rakhiray11223344
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

java lab

The document contains Java programming examples for various tasks, including printing 'Hello World', adding two numbers, swapping numbers, converting between integers and binary, calculating factorials, adding complex numbers, calculating simple interest, printing Pascal's Triangle, and finding the sum of Fibonacci series. Each example includes the Java code and the corresponding output. These examples serve as practical demonstrations of basic programming concepts in Java.

Uploaded by

rakhiray11223344
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Write Hello World Program in Java

public class HelloWorld {


public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

output

Hello, World!

2. Write a Program in Java to Add two Numbers

public class AddTwoNumbers {


public static void main(String[] args) {
int num1 = 5, num2 = 10, sum;
sum = num1 + num2;
System.out.println("Sum of these numbers: " + sum);
}
}

Output

Sum of these numbers: 15

3. Write a Program to Swap Two Numbers


public class SwapNumbers {
public static void main(String[] args) {
int a = 5, b = 10;
System.out.println("Before swapping: a = " + a + ", b = " + b);
int temp = a;
a = b;
b = temp;
System.out.println("After swapping: a = " + a + ", b = " + b);
}
}

Output

Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5

4. Write a Java Program to convert Integer numbers and Binary numbers


public class ConvertBinary {
public static void main(String[] args) {
int num = 10;
String binary = Integer.toBinaryString(num);
System.out.println("Binary of " + num + " is: " + binary);

String binaryNumber = "1010";


int decimal = Integer.parseInt(binaryNumber, 2);
System.out.println("Decimal of " + binaryNumber + " is: " + decimal);
}
}
Output Binary of 10 is: 1010
Decimal of 1010 is: 10
5. Write a Program to Find Factorial of a Number in Java

public class Factorial {


public static void main(String[] args) {
int num = 5, factorial = 1;
for (int i = 1; i <= num; ++i) {
factorial *= i;
}
System.out.println("Factorial of " + num + " is: " + factorial);
}
}

Output:- Factorial of 5 is: 120


1 * 2 * 3 * 4 * 5 = 120

6. Write a Java Program to Add two Complex Numbers


public class ComplexNumber {
double real, imaginary;

ComplexNumber(double r, double i) {
this.real = r;
this.imaginary = i;
}

public static ComplexNumber add(ComplexNumber c1, ComplexNumber c2) {


return new ComplexNumber(c1.real + c2.real, c1.imaginary + c2.imaginary);
}

public static void main(String[] args) {


ComplexNumber c1 = new ComplexNumber(2.5, 3.5);
ComplexNumber c2 = new ComplexNumber(1.5, 2.5);
ComplexNumber sum = add(c1, c2);
System.out.println("Sum: " + sum.real + " + " + sum.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.

7. Write a Program to Calculate Simple Interest in Java


public class SimpleInterest {
public static void main(String[] args) {
double principal = 1000, rate = 5, time = 2;
double simpleInterest = (principal * rate * time) / 100;
System.out.println("Simple Interest: " + simpleInterest);
}
}

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

9. Write a Program to Find Sum of Fibonacci Series Number


public class FibonacciSeries {
public static void main(String[] args) {
int n = 10, firstTerm = 0, secondTerm = 1, sum = 0;
System.out.println("Fibonacci Series till " + n + " terms:");
for (int i = 1; i <= n; ++i) {
System.out.print(firstTerm + " ");
sum += firstTerm;
int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
}
System.out.println("\nSum of Fibonacci Series: " + sum);
}
}

Output
Fibonacci Series till 10 terms:
0 1 1 2 3 5 8 13 21 34
Sum of Fibonacci Series: 88

You might also like