Assignment - 2
Assignment - 2
import java.util.Scanner;
public class PrimeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
boolean isPrime = true;
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime && num > 1) {
System.out.println(num + " is a prime number.");
} else {
System.out.println(num + " is not a prime number.");
}
}
}
OUTPUT:
class SecondLargest {
public static void main(String[] args) {
int[] arr = {3, 5, 1, 2, 4};
int largest = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
for (int num : arr) {
if (num > largest) {
second = largest;
largest = num;
} else if (num > second && num != largest) {
second = num;
}
}
System.out.println("Second Largest: " + second);
}
}
OUTPUT:
3. Write a Java program to count the number of vowels and consonants in a given string.
PROGRAM:
import java.util.Scanner;
public class VowelConsonantCount {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine().toLowerCase();
int vowels = 0, consonants = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= 'a' && ch <= 'z') {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowels++;
} else {
consonants++;
}
}
}
System.out.println("Vowels: " + vowels);
System.out.println("Consonants: " + consonants);
}
}
OUTPUT:
OUTPUT:
OUTPUT:
OUTPUT:
7. Write a Java program to check if a given string is a palindrome.
PROGRAM:
import java.util.Scanner;
public class PalindromeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine().toLowerCase();
String reversed = "";
for (int i = str.length() - 1; i >= 0; i--) {
reversed += str.charAt(i);
}
if (str.equals(reversed)) {
System.out.println(str + " is a palindrome.");
} else {
System.out.println(str + " is not a palindrome.");
}
}
}
OUTPUT:
9. Write a program to find the GCD (Greatest Common Divisor) of two numbers.
PROGRAM:
import java.util.Scanner;
public class GCD {
public static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();
scanner.close();
int gcdResult = gcd(num1, num2);
System.out.println("The GCD of " + num1 + " and " + num2 + " is " + gcdResult);
}
}
OUTPUT:
10. Write a program to implement the Fibonacci sequence up to n terms using iteration.
PROGRAM:
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = sc.nextInt();
int a = 0, b = 1;
System.out.print("Fibonacci sequence: ");
for (int i = 0; i < n; i++) {
System.out.print(a + " ");
int next = a + b;
a = b;
b = next;
}
}
}
OUTPUT:
PROGRAM:
import java.util.HashSet;
import java.util.Scanner;
public class RemoveDuplicates {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.print("Enter the elements: ");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
HashSet<Integer> set = new HashSet<>();
for (int i = 0; i < n; i++) {
set.add(arr[i]);
}
System.out.println("Array after removing duplicates: " + set);
}
}
OUTPUT:
13. Write a Java program to find the largest and smallest numbers in an array.
PROGRAM:
import java.util.Scanner;
public class LargestSmallest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.print("Enter the elements: ");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int largest = arr[0];
int smallest = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
if (arr[i] < smallest) {
smallest = arr[i];
}
}
System.out.println("Largest: " + largest);
System.out.println("Smallest: " + smallest);
}
}
OUTPUT:
return -1;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.print("Enter the elements (sorted): ");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.print("Enter the element to search: ");
int target = sc.nextInt();
int result = binarySearch(arr, target);
if (result == -1) {
System.out.println("Element not found.");
} else {
System.out.println("Element found at index: " + result);
}
}
}
OUTPUT:
16. Write a program to demonstrate the concept of method overloading.
PROGRAM:
public class MethodOverloading {
public static int add(int a, int b) {
return a + b;
}
public static double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
System.out.println("Sum of integers: " + add(9, 18));
System.out.println("Sum of doubles: " + add(9.5, 10.6));
}
}
OUTPUT:
17. Write a program to read a file and count the number of lines in it.
PROGRAM:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class count{
public static void main(String[] args) {
String filePath = "C:\\Users\\PC\\Desktop\\so.txt";
int lineCount = 0;
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = br.readLine()) != null) {
lineCount++;
}
System.out.println("Number of lines: " + lineCount);
} catch (IOException e) {
System.out.println("Error reading file.");
}
}
}
OUTPUT:
OUTPUT:
PROGRAM:
public class Stack {
private int[] stack;
private int top;
public Stack(int size) {
stack = new int[size];
top = -1;
}
public void push(int value) {
if (top == stack.length - 1) {
System.out.println("Stack Overflow");
} else {
stack[++top] = value;
System.out.println(value + " pushed to stack");
}
}
public void pop() {
if (top == -1) {
System.out.println("Stack Underflow");
} else {
System.out.println(stack[top--] + " popped from stack");
}
}
public static void main(String[] args) {
Stack stack = new Stack(5);
stack.push(10);
stack.push(20);
stack.pop();
}
}
OUTPUT:
20. Write a Java program to create a custom exception and handle it.
PROGRAM:
public class CustomException {
public static void main(String[] args) {
try {
int number = -9;
if (number < 0) {
throw new Exception("Negative number not allowed.");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
OUTPUT: