Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Java_Programs

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.

Uploaded by

21CS35 R. Kaviya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java_Programs

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.

Uploaded by

21CS35 R. Kaviya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Programs Collection

Check if Number is Positive or Negative


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());
}
}

Find the First Non-Repeating Character


import java.util.LinkedHashMap;
import java.util.Map;

public class FirstNonRepeatingCharacter {


public static char findFirstNonRepeating(String str) {
Map<Character, Integer> countMap = new LinkedHashMap<>();
for (char ch : str.toCharArray()) {
countMap.put(ch, countMap.getOrDefault(ch, 0) + 1);
}
for (Map.Entry<Character, Integer> entry : countMap.entrySet()) {
if (entry.getValue() == 1) return entry.getKey();
}
return '_';
}
public static void main(String[] args) {
System.out.println("First Non-Repeating Character: " + findFirstNonRepeating("swiss"));
}
}

You might also like