[Go to site: main page, start]

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

Java Anagram Solver Example

Java with online

Uploaded by

Stark
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 views8 pages

Java Anagram Solver Example

Java with online

Uploaded by

Stark
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

Basic to Advanced Java programs

1. Reverse a String
public class ReverseString {

public staticvoid main(Stringl]args) {

String str="Automation";

StringBuilderreversed =new StringBuilder(str).reverse):

[Link](reversed);

2. Check for Palindrome


public class Palindrome {

public staticvoid main(Stringl] args) {

String str ="madam";


String reversed =new StringBuilder(str).reverse).toString0;
[Link]([Link](reversed);

3. Fibonacci Series
public class Fibonacci {

public staticvoid main(Stringl]args) {

int n =10, num1 =0, num2 =1;


[Link]("FibonacciSeries: "+num1 +"," +num2);
=
for(int i 2;i<n; i++) {

int num3 = num1 + num2;

[Link](", + num3);
num1 = num2; num2 =num3;
4. Factorial of a Number
public class Factorial {

public static void main(Stringll args) {

int num = 5, factorial= 1;


for (int =1; <=num; i++){
i
i
factorial *= i;

[Link](factorial);
[Link] Number Check
public class PrimeCheck {
public staticvoid main(Stringl]args) {

int num =11;

boolean isPrime = true;

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


i {

if (num % i ==0){

isPrime = false;

break;

[Link](isPrime);

[Link] Vowels and Consonants


public class VowelConsonantCount {

public staticvoid main(Stringl]args) {

String str= "Automation";


int vowels = 0, consonantS = 0;
{
for (char c:str.toCharArray0)

if ("aeiouAEIOU".indexOf(c)!=-1){

vowels++;

}else if ([Link](c)) {

consonants++;

[Link] ("Vowels:" + vowels +", Consonants:" + consonants);

[Link] an Array
[Link];

public class SortArray {

public static void main(Stringl] args) {

int[] arr ={5, 2, 8, 1, 3};


[Link](arr);

[Link] ([Link](arr):

8. Merge Two Arrays


import [Link];public class

MergeArrays {
public static void main(Stringl] args) {

int[] arr1 = {1, 3, 5}; intl] arr2 = {2, 4, 6}; int[] merged = new
int[[Link] + [Link]]; [Link](arr1, 0,

merged, 0, [Link]); [Link](arr2, 0, merged,

[Link], [Link]);
[Link] (Arrays. toString(merged));

9. Find the Largest Element in an Array


public class LargestInArray {

public staticvoid main(Stringl]args) {

int[] arr ={1, 3, 5, 7, 9};


int largest = arr[0]; for

(int num:arr) {

if (num > largest) {

largest =num;
}

[Link](largest);

10. Remove Duplicates from an Array


import [Link];

public class RemoveDuplicates {

public static void main(Stringl] args) {

int[] arr ={1, 2, 2, 3, 4, 4};


HashSet<Integer> set =new HashSet<>0;

for (int num :


arr) {

[Link](num);

[Link](set);
[Link] for Anagram
import [Link];

public class AnagramCheck {

public static void main(Stringl] args){

String str1="listen",str2 ="silent";


char[] arr1= str1.toCharArray0;

charl] arr2 =[Link]);


[Link](arr1);

[Link](arr2);

[Link]([Link](arr1, ar2));

[Link] the Number of Digits in a Number


public class CountDigits {

public static void main(Stringl] args){

int num= 12345;

int count = String,valueOf(num).length):


[Link](count);

16. Print the Prime Numbers in a Range


public class PrimeInRange {

public static void main(Stringl] args){


int start =10, end = 50;
for (int num =start; num <= end; num++) {

boolean isPrime =true;


for (int =2; i <= [Link](num); i++){
i
if (num % i == 0){

isPrime = false;

break;

if (isPrime && num> 1){

[Link](num +"");

}
[Link] the Second Largest Element in an Array
public class SecondLargest

public staticvoid main(Stringl] args) {

int[] arr = {12,35,1, 10,34, 1};

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;

[Link](second);

}
18. Swap Two Numbers
public class SwapNumbers {

public staticvoid main(Stringl]args) {

int a =5, b= 10; a =a + b; b =a - b; a =a


- b; [Link]("a: " + a +", b:"+
b);

19. Print the Pascal's Triangle


public class PascalsTriangle {
public static void main(Stringl] args) {

int rows= 5;

for(int i =0; i<rows; i++) {


int num =1;

[Link]("%" + (rows- i) *2+"s", ""):

for (int j= 0; j <= j++){ i;

[Link]("%4d", num);

num = num * (i -i)/+1);

}
[Link]);

20. Find the Missing Number in an Array


public class MissingNumber{

public static void main(Stringl] args) {


int[] arr ={1, 2, 4, 5, 6};
int n = [Link] +1;
int total =n*(n+1)/ 2;

for (int num : arr) {

total -= num;

[Link](total); 21. Convert Decimal to Binary


public class DecimalToBinary {
public static void main(String[] args) {

int num=10;

String binary = [Link](num);


[Link](binary);

[Link] for Perfect Number


public class PerfectNumber{

public static void main(String[]args) {

int num= 28, sum = 0;


for (int i= 1; i<= num/ 2; i++) {
if (num % ==0){
i
Sum += ;

[Link](num ==sum);

Common questions

Powered by AI

Integer swapping without a temporary variable uses arithmetic operations: a = a + b, b = a - b, and then a = a - b. This technique effectively swaps values as it initially combines them into one number, then isolates each original number through subtraction. The integer overflows are avoided because of the limited operation scope .

The Java program calculates the factorial using an iterative loop, multiplying sequentially up to the number specified. While this approach efficiently computes smaller factorials, it could face scalability issues for larger numbers due to time complexity and potential integer overflow, as Java's int type has a maximum value limit .

The Java program uses StringBuilder's reverse method to reverse the string. This approach is more efficient than using simple string concatenation due to the mutable nature of StringBuilder, which allows modifications (like appends) without creating new string objects with each operation, reducing time complexity .

The Java program converts strings to character arrays to check for anagrams by sorting the arrays and comparing them. This approach utilizes sorting to rearrange characters into a canonical order, making it straightforward to compare whether two strings contain the same characters in any order .

The Java program checks if a number is prime by iterating from 2 up to the square root of the number. If any divisor is found, it sets isPrime to false. This method is efficient because if n = a * b, then one of a or b must be less than or equal to the square root of n, reducing unnecessary checks .

Using a HashSet to remove duplicates leverages its property of having only unique elements, which means duplicates are inherently removed. This process has an average time complexity of O(n), offering substantial performance improvement over a potential O(n^2) approach where each element is checked against all others .

The Arrays.sort method in Java is based on Dual-Pivot Quicksort for primitives, offering O(n log n) time complexity for average cases. This is generally preferred over manual implementations because it is highly optimized and less error-prone, leveraging native platform features and optimizations that may not be accessible in a custom sort .

The Java program determines a perfect number by summing its proper divisors (numbers less than itself that divide it evenly) and checking if the sum equals the number. A perfect number equals the sum of its divisors excluding itself. The program iteratively checks divisors up to half the number, reflecting its divisor-sum property .

The formula finds the missing number by calculating the expected sum of the series (using n*(n+1)/2) and subtracting the actual sum of the array's elements from it. This method assumes the array contains natural numbers from 1 to n with exactly one number missing, guaranteeing a single missing number can be precisely identified .

The Java program calculates each row of Pascal's Triangle using a loop where it sets the initial element as 1. The program follows the combination formula C(n, k) = C(n, k-1) * (n-k+1)/k to populate each element based on values from the previous row. This mathematical approach effectively builds the triangle row by row, as each element relies only on its adjacent values from the prior row .

You might also like