[Go to site: main page, start]

0% found this document useful (0 votes)
14 views7 pages

Class 11 ISC Java Programs Large Text

The document contains multiple Java programs that demonstrate various functionalities such as finding palindrome words in a sentence, performing operations on square matrices, sorting names in descending order, reversing a number using recursion, calculating the sum of squares of digits, and checking if a number is perfect based on the sum and product of its digits.

Uploaded by

diwakar04122007
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)
14 views7 pages

Class 11 ISC Java Programs Large Text

The document contains multiple Java programs that demonstrate various functionalities such as finding palindrome words in a sentence, performing operations on square matrices, sorting names in descending order, reversing a number using recursion, calculating the sum of squares of digits, and checking if a number is perfect based on the sum and product of its digits.

Uploaded by

diwakar04122007
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

1.

Palindrome Words from a Sentence

import [Link].*;

class PalindromeWords
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a sentence:");
String str = [Link]();

String words[] = [Link](" ");

[Link]("Palindrome words are:");

for(int i = 0; i < [Link]; i++)


{
if(isPalindrome(words[i]))
[Link](words[i]);
}
}

static boolean isPalindrome(String word)


{
String rev = "";

for(int i = [Link]()-1; i >= 0; i--)


rev = rev + [Link](i);

if([Link](rev))
return true;
else
return false;
}
}
2. Square Matrix Operations

import [Link].*;

class MatrixOperations
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);

[Link]("Enter size of matrix: ");


int n = [Link]();

int a[][] = new int[n][n];

[Link]("Enter matrix elements:");


for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
a[i][j] = [Link]();

for(int i=0;i<n;i++)
{
for(int j=0;j<n-1;j++)
{
for(int k=0;k<n-j-1;k++)
{
if(a[i][k] > a[i][k+1])
{
int temp = a[i][k];
a[i][k] = a[i][k+1];
a[i][k+1] = temp;
}
}
}
}

for(int j=0;j<n;j++)
{
for(int i=0;i<n-1;i++)
{
for(int k=0;k<n-i-1;k++)
{
if(a[k][j] < a[k+1][j])
{
int temp = a[k][j];
a[k][j] = a[k+1][j];
a[k+1][j] = temp;
}
}
}
}

for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(a[i][i] > a[j][j])
{
int temp = a[i][i];
a[i][i] = a[j][j];
a[j][j] = temp;
}
}
}

for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(a[i][n-1-i] < a[j][n-1-j])
{
int temp = a[i][n-1-i];
a[i][n-1-i] = a[j][n-1-j];
a[j][n-1-j] = temp;
}
}
}

[Link]("Final Matrix:");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
[Link](a[i][j] + " ");
[Link]();
}
}
}
3. Names in Descending Order

import [Link].*;

class NameSort
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);

String name[] = new String[5];

[Link]("Enter 5 names:");
for(int i=0;i<5;i++)
name[i] = [Link]();

for(int i=0;i<4;i++)
{
for(int j=0;j<4-i;j++)
{
if(name[j].compareTo(name[j+1]) < 0)
{
String temp = name[j];
name[j] = name[j+1];
name[j+1] = temp;
}
}
}

[Link]("Names in Descending Order:");


for(int i=0;i<5;i++)
[Link](name[i]);
}
}
4. Reverse Number Using Recursion

import [Link].*;

class ReverseNumber
{
static int rev = 0;

static int reverse(int n)


{
if(n == 0)
return rev;

rev = rev * 10 + n % 10;


return reverse(n/10);
}

public static void main(String args[])


{
Scanner sc = new Scanner([Link]);
[Link]("Enter number: ");
int num = [Link]();

int result = reverse(num);


[Link]("Reverse = " + result);
}
}
5. Sum of Squares of Digits

import [Link].*;

class SquareSum
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter number: ");
int n = [Link]();

int sum = 0;

while(n > 0)
{
int d = n % 10;
sum = sum + d*d;
n = n/10;
}

[Link]("Sum of squares = " + sum);


}
}
6. Check Perfect Number (Sum of Digits = Product
of Digits)

import [Link].*;

class PerfectCheck
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter number: ");
int n = [Link]();

int sum = 0;
int product = 1;
int temp = n;

while(temp > 0)
{
int d = temp % 10;
sum = sum + d;
product = product * d;
temp = temp/10;
}

if(sum == product)
[Link]("Perfect Number");
else
[Link]("Not Perfect Number");
}
}

Common questions

Powered by AI

The PerfectCheck class defines a number as "perfect" if the sum of its digits equals the product of its digits. It calculates both by iterating through the digits of the number with modulo and division operations and compares the sum and product. If they match, it prints "Perfect Number"; otherwise, "Not Perfect Number." This criteria is specific and not in line with traditional perfect number definitions .

The NameSort class sorts an array of five names in descending order by using a nested loop with the `compareTo` method. It swaps adjacent elements if they are in incorrect order (i.e., if `name[j].compareTo(name[j+1]) < 0`) using the bubble sort technique, resulting in a list sorted from highest to lowest lexicographically .

The NameSort class employs a bubble sort algorithm, effective for small datasets like the fixed array of five names but inefficient for larger datasets due to its O(n²) complexity. It assumes a consistent number of inputs, doesn't handle varying input sizes, and presumes lexicographic comparison suffices for all string data, not accounting for locale-specific rules like accented characters or culturally dependent name ordering .

The MatrixOperations class performs several operations on a square matrix: sorting rows in ascending order, columns in descending order, sorting the main diagonal in ascending order, and sorting the anti-diagonal in descending order. The result is a modified matrix where each row is sorted in ascending order, columns in descending order, and diagonals according to the specified criteria .

The ReverseNumber class implements recursion to reverse an integer by continuously shifting existing digits and appending the last digit of the remaining number, recursively repeating this until there are no digits left. A potential drawback is that this recursive approach might lead to a stack overflow for very large numbers due to excessive recursion depth .

The use of `equalsIgnoreCase` for palindrome checking allows the program to recognize palindromes regardless of letter casing, increasing flexibility and usability for the user. This ensures that mixed-case palindromes, like 'Racecar', are correctly identified, enhancing the robustness of the palindrome detection logic .

The bubble sort method used for sorting rows and columns in the MatrixOperations class is inefficient for larger matrices due to its O(n²) complexity for each sorting pass. This results in high computational costs, especially for large matrices, as each row and column require separate sorting passes, making it suboptimal for scenarios needing high performance .

The code checks if a word is a palindrome by reversing the word and comparing it with the original. It uses the `isPalindrome` method, which iterates backward through the word to create a reversed version, then compares the reversed string with the original using `equalsIgnoreCase` to handle case-sensitivity .

The recursive implementation appends the last digit to a reversed number by progressively reducing the original number until it reaches zero, ensuring correct placement of digits by building the reversed number from the least significant digit. Challenges with this method include managing stack depth limits and handling large integers that could result in integer overflow or excessive recursion depth .

The SquareSum class calculates the sum of the squares of digits by iterating over each digit of the number using modulo (`%`) to extract the last digit, squaring it, and adding it to a sum total. The loop continues until all digits have been processed. This method assumes the input number is non-negative, as it does not handle negative numbers .

You might also like