0% found this document useful (0 votes)
11 views20 pages

Full 20 A4 Java Programs Class11

The document contains a series of Java programs that perform various string and array manipulations, including checking for palindromes, counting vowels and consonants, checking for anagrams, sorting arrays, and matrix operations. Each program includes user input for demonstration and provides sample outputs for clarity. The programs cover a wide range of functionalities, from basic string operations to more complex matrix calculations.

Uploaded by

4010vandana
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)
11 views20 pages

Full 20 A4 Java Programs Class11

The document contains a series of Java programs that perform various string and array manipulations, including checking for palindromes, counting vowels and consonants, checking for anagrams, sorting arrays, and matrix operations. Each program includes user input for demonstration and provides sample outputs for clarity. The programs cover a wide range of functionalities, from basic string operations to more complex matrix calculations.

Uploaded by

4010vandana
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/ 20

1.

String Palindrome Checker


import java.util.Scanner;
public class StringPalindrome {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = sc.nextLine();
String reversed = "";
for (int i = input.length() - 1; i >= 0; i--) {
reversed += input.charAt(i);
}
if (input.equalsIgnoreCase(reversed)) {
System.out.println(input + " is a palindrome.");
} else {
System.out.println(input + " is not a palindrome.");
}
sc.close();
}
}

Sample Output:
Input: madam
madam is a palindrome.
2. Count Vowels and Consonants
import java.util.Scanner;
public class VowelConsonantCounter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String sentence = sc.nextLine();
int vowels = 0, consonants = 0;
sentence = sentence.toLowerCase();
for (int i = 0; i < sentence.length(); i++) {
char ch = sentence.charAt(i);
if (Character.isLetter(ch)) {
if ("aeiou".indexOf(ch) != -1)
vowels++;
else
consonants++;
}
}
System.out.println("Vowels: " + vowels);
System.out.println("Consonants: " + consonants);
sc.close();
}
}

Sample Output:
Input: Hello World
Vowels: 3
Consonants: 7
3. Check Anagram Strings
import java.util.Arrays;
import java.util.Scanner;
public class AnagramCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first string: ");
String str1 = sc.nextLine().toLowerCase();
System.out.print("Enter second string: ");
String str2 = sc.nextLine().toLowerCase();
char[] a = str1.toCharArray();
char[] b = str2.toCharArray();
Arrays.sort(a);
Arrays.sort(b);
if (Arrays.equals(a, b)) {
System.out.println("The strings are anagrams.");
} else {
System.out.println("The strings are not anagrams.");
}
sc.close();
}
}

Sample Output:
Input: listen, silent
The strings are anagrams.
4. Sort an Array (Bubble Sort)
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of elements: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter " + n + " elements:");
for (int i = 0; i < n; i++) arr[i] = sc.nextInt();
for (int i = 0; i < n - 1; i++) {
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;
}
}
}
System.out.print("Sorted array: ");
for (int i : arr) System.out.print(i + " ");
sc.close();
}
}

Sample Output:
Input: 5 elements: 4 3 1 5 2
Sorted array: 1 2 3 4 5
5. Find Second Largest Element
import java.util.Scanner;
public class SecondLargest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of elements: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter elements:");
for (int i = 0; i < n; i++) arr[i] = sc.nextInt();
int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
for (int num : arr) {
if (num > first) {
second = first;
first = num;
} else if (num > second && num != first) {
second = num;
}
}
System.out.println("Second largest number: " + second);
sc.close();
}
}

Sample Output:
Input: 6 elements: 10 20 40 30 50 50
Second largest number: 40
6. Transpose of a Matrix
import java.util.Scanner;
public class MatrixTranspose {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] mat = new int[3][3];
System.out.println("Enter 3x3 matrix:");
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
mat[i][j] = sc.nextInt();
System.out.println("Transpose of matrix:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(mat[j][i] + " ");
}
System.out.println();
}
sc.close();
}
}

Sample Output:
Input: 1 2 3 4 5 6 7 8 9
Transpose:
1 4 7
2 5 8
3 6 9
7. Sum of Diagonals in a Matrix
import java.util.Scanner;
public class DiagonalSum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] mat = new int[3][3];
System.out.println("Enter 3x3 matrix:");
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
mat[i][j] = sc.nextInt();
int primary = 0, secondary = 0;
for (int i = 0; i < 3; i++) {
primary += mat[i][i];
secondary += mat[i][2 - i];
}
System.out.println("Primary Diagonal Sum: " + primary);
System.out.println("Secondary Diagonal Sum: " + secondary);
sc.close();
}
}

Sample Output:
Input: 1 2 3 4 5 6 7 8 9
Primary Diagonal: 15
Secondary Diagonal: 15
8. Remove Duplicates from a String
import java.util.Scanner;
public class RemoveDuplicates {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = sc.nextLine();
String result = "";
for (int i = 0; i < input.length(); i++) {
if (result.indexOf(input.charAt(i)) == -1)
result += input.charAt(i);
}
System.out.println("Without duplicates: " + result);
sc.close();
}
}

Sample Output:
Input: programming
progamin
9. Count Words in a Sentence
import java.util.Scanner;
public class WordCount {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String sentence = sc.nextLine().trim();
if (sentence.isEmpty()) {
System.out.println("Word count: 0");
} else {
String[] words = sentence.split("\s+");
System.out.println("Word count: " + words.length);
}
sc.close();
}
}

Sample Output:
Input: BlueJ is an IDE for Java
Word count: 6
10. Sort Characters in a String
import java.util.Arrays;
import java.util.Scanner;
public class SortCharacters {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
char[] chars = str.toCharArray();
Arrays.sort(chars);
System.out.println("Sorted chars: " + new String(chars));
sc.close();
}
}

Sample Output:
Input: computer
Sorted chars: cemoprtu
11. Frequency of Each Character
import java.util.Scanner;
public class CharFrequency {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
int[] freq = new int[256];
for (char c : str.toCharArray()) {
freq[c]++;
}
for (int i = 0; i < freq.length; i++) {
if (freq[i] > 0) {
System.out.println((char)i + ": " + freq[i]);
}
}
sc.close();
}
}

Sample Output:
Input: success
s:3 u:1 c:2 e:1
12. Merge Two Sorted Arrays
import java.util.Scanner;
public class MergeSortedArrays {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of first array: ");
int n1 = sc.nextInt();
int[] a = new int[n1];
System.out.println("Enter sorted elements of first array:");
for (int i = 0; i < n1; i++) a[i] = sc.nextInt();
System.out.print("Enter size of second array: ");
int n2 = sc.nextInt();
int[] b = new int[n2];
System.out.println("Enter sorted elements of second array:");
for (int i = 0; i < n2; i++) b[i] = sc.nextInt();
int[] c = new int[n1 + n2];
int i=0, j=0, k=0;
while(i<n1 && j<n2) {
c[k++] = (a[i] < b[j]) ? a[i++] : b[j++];
}
while(i<n1) c[k++] = a[i++];
while(j<n2) c[k++] = b[j++];
System.out.print("Merged array: ");
for(int num : c) System.out.print(num + " ");
sc.close();
}
}

Sample Output:
Input: [1,3,5] & [2,4,6]
Merged array: 1 2 3 4 5 6
13. Rotate Array Elements Right
import java.util.Scanner;
public class RotateRight {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of elements: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter elements:");
for (int i = 0; i < n; i++) arr[i] = sc.nextInt();
int last = arr[n-1];
for(int i=n-1; i>0; i--) arr[i] = arr[i-1];
arr[0] = last;
System.out.print("Rotated array: ");
for(int num : arr) System.out.print(num + " ");
sc.close();
}
}

Sample Output:
Input: [10,20,30,40]
Rotated array: 40 10 20 30
14. Diagonal Difference in Matrix
import java.util.Scanner;
public class DiagonalDifference {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] mat = new int[3][3];
System.out.println("Enter 3x3 matrix:");
for (int i=0; i<3; i++)
for (int j=0; j<3; j++)
mat[i][j] = sc.nextInt();
int primary=0, secondary=0;
for(int i=0; i<3; i++){
primary += mat[i][i];
secondary += mat[i][2-i];
}
System.out.println("Difference: " + Math.abs(primary-secondary));
sc.close();
}
}

Sample Output:
Input: 11 2 4;4 5 6;10 8 -12
Difference: 15
15. Multiply Two Matrices
import java.util.Scanner;
public class MatrixMultiply {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter rows and columns of first matrix: ");
int r1 = sc.nextInt(), c1 = sc.nextInt();
int[][] A = new int[r1][c1];
System.out.println("Enter elements of first matrix:");
for(int i=0;i<r1;i++)
for(int j=0;j<c1;j++)
A[i][j] = sc.nextInt();
System.out.print("Enter columns of second matrix: ");
int c2 = sc.nextInt();
int[][] B = new int[c1][c2];
System.out.println("Enter elements of second matrix:");
for(int i=0;i<c1;i++)
for(int j=0;j<c2;j++)
B[i][j] = sc.nextInt();
int[][] C = new int[r1][c2];
for(int i=0;i<r1;i++)
for(int j=0;j<c2;j++)
for(int k=0;k<c1;k++)
C[i][j] += A[i][k] * B[k][j];
System.out.println("Product matrix:");
for(int[] row:C){
for(int val:row) System.out.print(val + " ");
System.out.println();
}
sc.close();
}
}

Sample Output:
Input: A(2x2) & B(2x2)
Product matrix: 4 4;10 8
16. Count Positive, Negative, Zero in Array
import java.util.Scanner;
public class CountNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of elements: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter elements:");
for(int i=0;i<n;i++) arr[i] = sc.nextInt();
int pos=0, neg=0, zero=0;
for(int num:arr){
if(num>0) pos++;
else if(num<0) neg++;
else zero++;
}
System.out.println("Positive: "+pos);
System.out.println("Negative: "+neg);
System.out.println("Zero: "+zero);
sc.close();
}
}

Sample Output:
Input: [0,-2,5,-7,0,3]
Positive:2 Negative:2 Zero:2
17. Check if String is Pangram
import java.util.Scanner;
public class PangramCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String str = sc.nextLine().toLowerCase().replaceAll("[^a-z]","");
boolean[] alpha = new boolean[26];
for(char c: str.toCharArray()){
alpha[c-'a'] = true;
}
boolean isPangram = true;
for(boolean b: alpha) if(!b) isPangram = false;
System.out.println("Is Pangram: "+ isPangram);
sc.close();
}
}

Sample Output:
Input: The quick brown fox jumps over the lazy dog
Is Pangram: true
18. Replace Vowels with * in a String
import java.util.Scanner;
public class ReplaceVowels {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
String result = "";
for(char c: str.toCharArray()) {
if("AEIOUaeiou".indexOf(c) != -1) result += '*';
else result += c;
}
System.out.println("Modified: " + result);
sc.close();
}
}

Sample Output:
Input: BlueJ IDE
Modified: Bl**J *D*
19. Count Digits, Alphabets and Special Characters
import java.util.Scanner;
public class CountChars {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
int digits=0, letters=0, special=0;
for(char c: str.toCharArray()) {
if(Character.isDigit(c)) digits++;
else if(Character.isLetter(c)) letters++;
else special++;
}
System.out.println("Letters: "+letters);
System.out.println("Digits: "+digits);
System.out.println("Special: "+special);
sc.close();
}
}

Sample Output:
Input: Hello123@ICSE!
Letters:9 Digits:3 Special:3
20. Reverse Each Word in a Sentence
import java.util.Scanner;
public class ReverseWords {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String[] words = sc.nextLine().split(" ");
System.out.print("Reversed words: ");
for(String w: words) {
String rev = "";
for(int i=w.length()-1;i>=0;i--) rev += w.charAt(i);
System.out.print(rev + " ");
}
sc.close();
}
}

Sample Output:
Input: Hello World
Reversed words: olleH dlroW

You might also like