Practical File of Object-Oriented Programming With Java
Practical File of Object-Oriented Programming With Java
Java (BCS403)
ALIGARH
Output:
Hello, World!
Output:
Sum: 15
3. Factorial of a Number
{
factorial *= i;
}
System.out.println("Factorial of " + num + "
= " + factorial);
}
}
Output:
Factorial of 5 = 120
Output:
29 is odd.
Output:
}
}
Output:
29 is a prime number.
Output:
8. Reverse a String
{
reversed += str.charAt(i);
}
System.out.println("Reversed string: " +
reversed);
}
}
Output:
Output:
10.Simple Calculator
import java.util.Scanner;
double result;
switch(operator) {
case '+':
result = first + second;
break;
case '-':
result = first - second;
break;
case '*':
result = first * second;
break;
case '/':
result = first / second;
break;
default:
System.out.printf("Error! operator is
not correct");
return;
}
Output (example):
Advanced Programs
11.Bubble Sort
{
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
}
}
}
Output:
Sorted array:
11 12 22 25 34 64 90
12.Binary Search
}
}
Output:
class LinkedList
{
Node head;
Node(int d)
{
data = d;
next = null;
}
}
list.head.next = second;
second.next = third;
list.printList();
}
}
Output:
1 2 3
14.HashMap Example
java
Copy code
import java.util.HashMap;
map.put("One", 1);
map.put("Two", 2);
map.put("Three", 3);
Output:
yaml
Copy code