Program - 1: 1. Write A Java To Print Hello World
Program - 1: 1. Write A Java To Print Hello World
Import java.util.*;
class helloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Import java.util.Scanner;
class Circle {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the radius of the circle: ");
double radius = scanner.nextDouble(); double
area = Math.PI * radius * radius; double perimeter
= 2 * Math.PI * radius; System.out.println("Area of
the circle: " + area); System.out.println("Perimeter
(Circumference) of the
circle: " + perimeter);
scanner.close();
} }
Output :
Enter the radius of the circle: 2
Area of the circle: 12.566370614359172
Perimeter (Circumference) of the circle: 12.566370614359172
Program – 3
3. Write a java program to swap two numbers without using a third variable .
import java.util.Scanner;
public class lab {
public static void main(String[] args) {
int a = 5;
int b = 10;
System.out.println("Before swapping: a = " + a + ", b
="+b);
a=a+b; b=a-b; a=a-b; System.out.println("After
swapping: a = " + a + ", b =
" + b);
}
}
Output :
Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5
Program – 4
4. Write a java program to check if vowel is present in a string
import
java.util.Scanner;
public
public static void class
main(String[] args) {
CheckVowel
Scanner { scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
boolean vowelPresent = false;
input = input.toLowerCase();
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch ==
'o' || ch == 'u') {
vowelPresent = true;
break;
}
}
if (vowelPresent) {
System.out.println("Vowel is present in the
string.");
} else {
System.out.println("No vowel is present in the
string.");
}
scanner.close();
}
}
Output:
Enter a string: krishna
Vowel is present in the string.
Program – 5
5. write a java program to print the element of array.
}
}
Output: Elements of
the array:
Element at index 0: 1
Element at index 1: 2
Element at index 2: 3
Element at index 3: 4
Element at index 4: 5
Program – 6
6. Write a java program to print largest element in array.
import java.util.Scanner;
public class LargestElement {
public static void main(String[] args) {
int[] array = {10, 5, 20, 15, 30};
int largest = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > largest) {
largest = array[i];
}
}
System.out.println("Largest element in the array: " +
largest);
}
}
Output:
Largest element in the array: 30
Program – 7
7. Write a java program to reverse a string.
import java.util.Scanner;
import java.util.Scanner;
public class StringReversal
{ public static void main(String[] args) {
String input = "Hello, world!";
String reversed = reverseString(input);
System.out.println("Original string: " + input);
System.out.println("Reversed string: " + reversed);
}
public static String reverseString(String str) {
char[] charArray = str.toCharArray();
int left = 0;
int right = charArray.length - 1;
while (left < right) {
char temp = charArray[left];
charArray[left] = charArray[right];
charArray[right] = temp;
left++;
right--;
}
return new String(charArray);
}
}
public static void main(String[] args) {
int[] array = {10, 5, 20, 15, 30};
int largest = array[0];
import java.util.Scanner;
public class lab {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
if (isPalindrome(input)) {
System.out.println("The string \"" + input + "\"
is a palindrome.");
} else {
System.out.println("The string \"" + input + "\"
is not a palindrome.");
}
scanner.close();
}
public static boolean isPalindrome(String str) {
str = str.replaceAll("\\s+", "").toLowerCase();
int left = 0;
int right = str.length() - 1;
while (left < right) {
if (str.charAt(left) != str.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
}
Output:
1. Enter a string: Krishna
The string "Krishna" is not a palindrome.
Import java.util.Scanner;
class Vehicle {
public void drive() {
System.out.println("Driving a vehicle.");
}
}
class Car extends Vehicle {
public void drive() {
System.out.println("Repairing a car.");
}
}
public class VehicleDemo {
public static void main(String[] args) {
Vehicle vehicle = new Vehicle();
vehicle.drive();
Car car = new Car();
car.drive();
}
}
Output:
Driving a vehicle.
Repairing a car.
Program – 10
10.Write a java program to implement Fibonacci series using recursion.
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of terms in the
Fibonacci series: ");
int n = scanner.nextInt();
System.out.println("Fibonacci series:");
for (int i = 0; i < n; i++) {
System.out.print(fibonacci(i) + " ");
}
scanner.close();
}
public static int fibonacci(int n) {
if (n <= 1) {
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
}
Output:
array[k] = rightArray[j];
j++;
}
k++;
}
while (i < n1) {
array[k] = leftArray[i];
i++; k++;
}
while (j < n2) {
array[k] = rightArray[j];
j++; k++;
}
}
public static void printArray(int[] array) {
for (int value : array) {
System.out.print(value + " ");
}
System.out.println();
}
}
Output :
Original array:
121113567
Sorted array:
567111213
Program – 12
12.Write a java program to illustrate quick sort
import java.util.Scanner;
public class QuickSort {
public static void main(String[] args) {
int[] array = {12, 11, 13, 5, 6, 7};
System.out.println("Original array:");
printArray(array);
quickSort(array, 0, array.length - 1);
System.out.println("\nSorted array:");
printArray(array);
}
public static void quickSort(int[] array, int low, int
high) {
if (low < high) {
int pivotIndex = partition(array, low, high);
quickSort(array, low, pivotIndex - 1);
quickSort(array, pivotIndex + 1, high);
}
}
public static int partition(int[] array, int low, int high)
{
int pivot = array[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (array[j] <= pivot) {
i++; int temp =
array[i]; array[i] =
array[j]; array[j] =
temp;
}
}
int temp = array[i + 1];
array[i + 1] = array[high];
array[high] = temp;
return i + 1;
}
public static void printArray(int[] array) {
for (int value : array) {
System.out.print(value + " ");
}
System.out.println();
}
}
Output:
Original array:
121113567
Sorted array:
567111213
Program – 13
12.Write a java program to implement Binary search.
import java.util.Scanner;
public class BinarySearch {
public static void main(String[] args) {
int[] array = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};
int target;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number to search: ");
target = scanner.nextInt();
int index = binarySearch(array, target);
if (index != -1) {
System.out.println("Element " + target + " found
at index " + index + ".");
} else {
System.out.println("Element " + target + " not
found in the array.");
}
scanner.close();
}
public static int binarySearch(int[] array, int target) {
int left = 0;
int right = array.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (array[mid] == target) {
return mid;
}
if (array[mid] < target) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
return -1;
}
}
Output:
Enter the number to search: 5
Element 5 found at index 1.
Program – 14
14.write a java program to check wheather number is prime or not ?
import
java.util.Scanner;
public
public static void class
main(String[] args) {
PrimeCheck
Scanner { scanner = new Scanner(System.in);
System.out.print("Enter a number to check if it's
prime: ");
int number = scanner.nextInt();
if (isPrime(number)) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime
number.");
}
scanner.close();
}
public static boolean isPrime(int number) {
if (number <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
}
Output:
import java.util.List;
public class OddNumberCheck {
public static void main(String[] args) {
List<Integer> numbers = List.of(3, 7, 11, 15, 9);
if (containsOnlyOddNumbers(numbers)) {
System.out.println("The list contains only odd
numbers.");
} else {
System.out.println("The list contains at least one
even number.");
}
}
public static boolean containsOnlyOddNumbers(List<Integer>
numbers) {
for (int number : numbers) {
if (number % 2 == 0) {
return false;
}
}
return true;
}
}