The document contains a collection of Java programs that demonstrate various functionalities, including checking if a number is positive or negative, determining if a number is even or odd, and finding the sum of the first N natural numbers. It also includes programs for finding the largest element in an array, checking if a string is a palindrome, reversing a string, and converting strings to uppercase and lowercase. Additionally, it features a program to find the first non-repeating character in a string.
The document contains a collection of Java programs that demonstrate various functionalities, including checking if a number is positive or negative, determining if a number is even or odd, and finding the sum of the first N natural numbers. It also includes programs for finding the largest element in an array, checking if a string is a palindrome, reversing a string, and converting strings to uppercase and lowercase. Additionally, it features a program to find the first non-repeating character in a string.
public class PositiveNegative { public static void main(String[] args) { int num = -5; System.out.println(num > 0 ? "Positive" : (num < 0 ? "Negative" : "Zero")); } }
Check if Number is Even or Odd
public class EvenOdd { public static void main(String[] args) { int num = 4; System.out.println(num % 2 == 0 ? "Even" : "Odd"); } }
Find the Sum of First N Natural Numbers
public class SumNaturalNumbers { public static void main(String[] args) { int n = 5, sum = n * (n + 1) / 2; System.out.println("Sum: " + sum); } }
Find the Largest Element in an Array
import java.util.Arrays; public class LargestElement { public static void main(String[] args) { int[] arr = {1, 5, 3, 9, 2}; System.out.println("Largest Element: " + Arrays.stream(arr).max().getAsInt()); } }
Check if a String is Palindrome
public class PalindromeCheck { public static boolean isPalindrome(String str) { return str.equals(new StringBuilder(str).reverse().toString()); } public static void main(String[] args) { System.out.println(isPalindrome("madam") ? "Palindrome" : "Not Palindrome"); } }
Reverse a String public class ReverseString { public static void main(String[] args) { String str = "hello"; System.out.println("Reversed: " + new StringBuilder(str).reverse().toString()); } }
Convert String to Uppercase
public class ToUpperCase { public static void main(String[] args) { String str = "hello world"; System.out.println("Uppercase: " + str.toUpperCase()); } }
Convert String to Lowercase
public class ToLowerCase { public static void main(String[] args) { String str = "HELLO WORLD"; System.out.println("Lowercase: " + str.toLowerCase()); } }