0% found this document useful (0 votes)
2 views10 pages

Java Basic to Advance Programes-@PDFMatrix

The document contains a collection of Java programming exercises ranging from basic to advanced levels. Each exercise includes a brief description and the corresponding Java code, covering topics such as string manipulation, number series, array operations, and mathematical checks. The document serves as a practical guide for learning and practicing Java programming concepts.
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)
2 views10 pages

Java Basic to Advance Programes-@PDFMatrix

The document contains a collection of Java programming exercises ranging from basic to advanced levels. Each exercise includes a brief description and the corresponding Java code, covering topics such as string manipulation, number series, array operations, and mathematical checks. The document serves as a practical guide for learning and practicing Java programming concepts.
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

coding_knowladge

Harry

Basic to Advanced Java programs


1. Reverse a String
public class ReverseString {

public static void main(String[] args) {

String str = "Automation";

StringBuilder reversed = new StringBuilder(str).reverse();

[Link](reversed);

}
2. Check for Palindrome
public class Palindrome {
}
public static void main(String[] args) {

String str = "madam";


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

3. Fibonacci Series
public class Fibonacci {

public static void main(String[] args) {

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


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

int num3 = num1 + num2;


[Link](", " + num3);
num1 = num2; num2 = num3;
}
}
} [Link]
4. Factorial of a Number
public class Factorial {
public static void main(String[] args) {

int num = 5, factorial = 1;


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

factorial *= i;

}
[Link](factorial);
5. Prime Number Check
}
} public class PrimeCheck {
public static void main(String[] args) {

int num = 11;

boolean isPrime = true;

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

if (num % i == 0) {

isPrime = false;

break;

[Link](isPrime);

}
}

6. Count Vowels and Consonants


public class VowelConsonantCount {

public static void main(String[] args) {

String str = "Automation";

[Link]
int vowels = 0, consonants = 0;

for (char c : [Link]()) {

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

vowels++;

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

consonants++;

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

7. Sort an Array
}

import [Link];

public class SortArray {

public static void main(String[] 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(String[] args) {

int[] arr1 = {1, 3, 5}; int[] arr2 = {2, 4, 6}; int[] merged = new

int[[Link] + [Link]]; [Link](arr1, 0,

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

[Link], [Link]); [Link]


[Link]([Link](merged));

}
}

9. Find the Largest Element in an Array


public class LargestInArray {

public static void main(String[] 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(String[] args) {

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


HashSet<Integer> set = new HashSet<>();
for (int num : arr) {
[Link](num);

}
[Link](set);

}
[Link]
}
11. Check if a Number is Armstrong
public class ArmstrongNumber {
public static void main(String[] args) {

int num = 153, sum = 0, temp = num;


while (temp != 0) {

int digit = temp % 10;


sum += [Link](digit, 3);
temp /= 10;

} 12. Reverse a Number


[Link](num == sum); public class ReverseNumber {
} public static void main(String[] args) {
}
int num = 12345, reversed = 0;

while (num != 0) {

reversed = reversed * 10 + num % 10;

num /= 10;

[Link](reversed);

13. Calculate GCD of Two Numbers


public class GCD {

public static void main(String[] args) {

int a = 60, b = 48;

while (b != 0) {

int temp = b; [Link]


b = a % b;
a = temp;

}
[Link](a);

14. Check for Anagram


import [Link];

public class AnagramCheck {

public static void main(String[] args) {

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

char[] arr1 = [Link]();

char[] arr2 = [Link]();

[Link](arr1);

[Link](arr2);

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

15. Count the Number of Digits in a Number


public class CountDigits {

public static void main(String[] args) {

int num = 12345;

int count = [Link](num).length();

[Link](count);

16. Print the Prime Numbers in a Range


public class PrimeInRange {

public static void main(String[] args) { [Link]


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

boolean isPrime = true;


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

if (num % i == 0) {
isPrime = false;
break;

}
if (isPrime && num > 1) {

[Link](num + " ");

}
} 17. Find the Second Largest Element in an Array
public class SecondLargest {
public static void main(String[] 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);

} [Link]
}

18. Swap Two Numbers


public class SwapNumbers {

public static void main(String[] 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(String[] args) {

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

int num = 1;
[Link]("%" + (rows - i) * 2 + "s", "");
for (int j = 0; j <= i; j++) {

[Link]("%4d", num);
num = num * (i - j) / (j + 1);

}
[Link]();

}
}

20. Find the Missing Number in an Array


public class MissingNumber {

public static void main(String[] args) {


[Link]
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);
}
}

22. Check 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 % i == 0) {

sum += i;

[Link](num == sum);

} [Link]
}

18. Swap Two Numbers


public class SwapNumbers {

public static void main(String[] 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(String[] args) {

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

int num = 1;
[Link]("%" + (rows - i) * 2 + "s", "");
for (int j = 0; j <= i; j++) {

[Link]("%4d", num);
num = num * (i - j) / (j + 1);

}
[Link]();

}
}

[Link]

You might also like