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

Java Programs

The document contains a thesis titled 'JAVA PROGRAMS' submitted for a Bachelor Degree in Information Technology, detailing various Java programming examples. It includes code snippets for basic operations such as Hello World, sum of two numbers, checking even or odd, factorial calculation, and more advanced topics like multithreading and networking. The thesis has been approved by external examiners, indicating successful defense of the work.

Uploaded by

nageshduje
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java Programs

The document contains a thesis titled 'JAVA PROGRAMS' submitted for a Bachelor Degree in Information Technology, detailing various Java programming examples. It includes code snippets for basic operations such as Hello World, sum of two numbers, checking even or odd, factorial calculation, and more advanced topics like multithreading and networking. The thesis has been approved by external examiners, indicating successful defense of the work.

Uploaded by

nageshduje
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

INDEX

1. Hello World
2. Sum of Two Numbers
3. Even or Odd
4. Factorial of a Number

5. Print Multiplication Table

6. Reverse a String

7. Check Prime Number

8. Find Largest of Three Numbers

9. Sum of Array Elements

10. Simple Calculator (Addition Only)

11. Simple Calculator Program (with user input)


12. Multithreading Example
13. Lambda Expression
14. Networking – Simple Server and Client
CERTIFICATE OF APPROVAL

Certified that the thesis entitled “JAVA PROGRAMS” submitted by name


of Dehradun Institute of Management & Technology, Dehradun for the
award of Bachelor Degree in (Information Technology (Bsc.IT)) has been
accepted by the external examiners and that the student has successfully
defended the work carried out, in the final examination.

Signature:

Name

(Supervisor)

Name:

(Internal examiner)

Signature:

Name:

(External Examiner)

Signature:

Name:

(Head of Department)
1. Hello World

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello, World!");

Output:

Hello, World!

2. Sum of Two Numbers


public class Sum {
public static void main(String[] args) {
int a = 5, b = 7;
int sum = a + b;
System.out.println("Sum: " + sum);
}
}

Output:

Sum: 12

3. Even or Odd

public class EvenOdd {


public static void main(String[] args) {
int num = 4;
if(num % 2 == 0)
System.out.println(num + " is Even");
else
System.out.println(num + " is Odd");
}
}

Output:
4 is Even

4. Factorial of a Number
public class Factorial {
public static void main(String[] args) {
int num = 5;
int fact = 1;
for(int i = 1; i <= num; i++) {
fact *= i;
}
System.out.println("Factorial: " + fact);
}
}

Output:

Factorial: 120

5. Print Multiplication Table


public class MultiplicationTable {
public static void main(String[] args) {
int num = 3;
for(int i = 1; i <= 10; i++) {
System.out.println(num + " x " + i + " = " + (num * i));
}
}
}

Output:

3 x 1 = 3
3 x 2 = 6
...
3 x 10 = 30

6. Reverse a String
public class ReverseString {
public static void main(String[] args) {
String str = "Java";
String reversed = "";
for(int i = str.length() - 1; i >= 0; i--) {
reversed += str.charAt(i);
}
System.out.println("Reversed: " + reversed);
}
}

Output:

Reversed: avaJ

7. Check Prime Number


public class PrimeCheck {
public static void main(String[] args) {
int num = 7;
boolean isPrime = true;
for(int i = 2; i <= num / 2; i++) {
if(num % i == 0) {
isPrime = false;
break;
}
}
if(isPrime)
System.out.println(num + " is a Prime number.");
else
System.out.println(num + " is not a Prime number.");
}
}

Output:

7 is a Prime number.

8. Find Largest of Three Numbers


public class Largest {
public static void main(String[] args) {
int a = 10, b = 25, c = 15;
int largest = a;

if(b > largest)


largest = b;
if(c > largest)
largest = c;

System.out.println("Largest: " + largest);


}
}

Output:

Largest: 25

9. Sum of Array Elements


public class ArraySum {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
int sum = 0;
for(int num : numbers) {
sum += num;
}
System.out.println("Sum of array: " + sum);
}
}

Output:

Sum of array: 15

10. Simple Calculator (Addition Only)


import java.util.Scanner;

public class SimpleCalculator {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter two numbers: ");
int a = scanner.nextInt();
int b = scanner.nextInt();
int result = a + b;
System.out.println("Sum: " + result);
scanner.close();
}
}

Output (Example):

Enter two numbers: 5 8


Sum: 13
11. Simple Calculator Program (with user input)

import java.util.Scanner;

public class SimpleCalculator {


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

// Input numbers
System.out.print("Enter first number: ");
double num1 = scanner.nextDouble();

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


double num2 = scanner.nextDouble();

// Choose operation
System.out.print("Enter an operator (+, -, *, /): ");
char operator = scanner.next().charAt(0);

double result;

switch (operator) {
case '+':
result = num1 + num2;
System.out.println("Result: " + result);
break;
case '-':
result = num1 - num2;
System.out.println("Result: " + result);
break;
case '*':
result = num1 * num2;
System.out.println("Result: " + result);
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
System.out.println("Result: " + result);
} else {
System.out.println("Error: Cannot divide by zero.");
}
break;
default:
System.out.println("Invalid operator.");
}

scanner.close();
}
}

Sample Output:
Enter first number: 10
Enter second number: 5
Enter an operator (+, -, *, /): *
Result: 50.0

12. Multithreading Example


class MyThread extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
public class ThreadExample {
public static void main(String[] args) {
MyThread t1 = new MyThread();
t1.start();
}
}

Output:
1
2
3
4
5

13. Lambda Expression


import java.util.*;
public class LambdaExample {
public static void main(String[] args) {
List<String> list = Arrays.asList("apple", "banana", "cherry");
list.forEach(s -> System.out.println(s.toUpperCase()));
}
}

Output:

APPLE
BANANA
CHERRY

4. Networking – Simple Server and Client


Server.java Clint.java

import java.io.*; import java.io.*;

import java.net.*; import java.net.*;

public class Server { public class Client {

public static void main(String[] args) throws public static void main(String[]
IOException { args) throws IOException {

ServerSocket ss = new Socket s = new


ServerSocket(6666); Socket("localhost", 6666);

Socket s = ss.accept(); DataOutputStream dout = new


DataOutputStream(s.getOutputStre
DataInputStream dis = new am());
DataInputStream(s.getInputStream());
dout.writeUTF("Hello from
String str = dis.readUTF(); client!");
System.out.println("Client: " + str); dout.flush();
ss.close(); dout.close();
} s.close();
} }
}

Output
Client: Hello from client!

You might also like