0% found this document useful (0 votes)
20 views18 pages

Program - 1: 1. Write A Java To Print Hello World

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

Program - 1: 1. Write A Java To Print Hello World

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

Program – 1

1. Write a java to print hello world.

Name: Devansh Yadav Rollno: 0863CS221052

Import java.util.*;
class helloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}

Output - Hello World


Program – 2
2. Write a java program to print area of circle and parameter of circle.

Name: Devansh Yadav Rollno: 0863CS221052

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 .

Name: Devansh Yadav Rollno: 0863CS221052

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

Name: Devansh Yadav Rollno: 0863CS221052

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.

Name: Devansh Yadav Rollno: 0863CS221052


import java.util.Scanner;
public class ArrayElement {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
System.out.println("Elements of the array:");
for (int i = 0; i < array.length; i++) {
System.out.println("Element at index " + i + ": "
+ array[i]);
}

}
}

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.

Name: Devansh Yadav Rollno: 0863CS221052

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.

Name: Devansh Yadav Rollno: 0863CS221052

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];

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:

Original string: Hello, world!


Reversed string: !dlrow ,olleH
Program – 8
8. Write a java program to check whether the string given is palindrome

Name: Devansh Yadav Rollno: 0863CS221052

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.

2. Enter a string: AKA


The string "AKA" is a palindrome.
Program – 9
9. Write a java program to implement a class vehicle with a method called
drive(). Create a sub class called car that overwrites the drive() method to
print repairing a car.

Name: Devansh Yadav Rollno: 0863CS221052

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.

Name: Devansh Yadav Rollno: 0863CS221052

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:

Enter the number of terms in the Fibonacci series: 5


Fibonacci series:
01123
Program – 11
11.write a java program that illustrate merge sort.

Name: Devansh Yadav Rollno: 0863CS221052

public class MergeSort {


public static void main(String[] args) {
int[] array = {12, 11, 13, 5, 6, 7};
System.out.println("Original array:");
printArray(array);
mergeSort(array, 0, array.length - 1);
System.out.println("\nSorted array:");
printArray(array);
}
public static void mergeSort(int[] array, int left, int
right) {
if (left < right) {
int mid = (left + right) / 2;
mergeSort(array, left, mid);
mergeSort(array, mid + 1, right);
merge(array, left, mid, right);
}
}
public static void merge(int[] array, int left, int mid,
int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int[] leftArray = new int[n1];
int[] rightArray = new int[n2];
for (int i = 0; i < n1; ++i) {
leftArray[i] = array[left + i];
}
for (int j = 0; j < n2; ++j) {
rightArray[j] = array[mid + 1 + j];
}
inti=0,j=0;
int k = left;
while (i < n1 && j < n2) {
if (leftArray[i] <= rightArray[j]) {
array[k] = leftArray[i]; i++; }
else {

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

Name: Devansh Yadav Rollno: 0863CS221052

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.

Name: Devansh Yadav Rollno: 0863CS221052

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 ?

Name: Devansh Yadav Rollno: 0863CS221052

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:

Enter a number to check if it's prime: 6


6 is not a prime number.
Program – 15
15. How do you check the list of numbers contain only odd numbers in Java.

Name: Devansh Yadav Rollno: 0863CS221052

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;
}
}

Output : The list contains only odd numbers.

You might also like