1-Reverse a string in Java?
public
class ReverseTajMahal {
public static void main(String[] args) {
String original = "TAJMAHAL";
String reversed = reverseString(original);
System.out.println("Original String: " +
original);
System.out.println("Reversed String: " +
reversed);
}
public static String reverseString(String in) {
if (in == null) {
return null;
}
//StringBuilder has a built-in reverse method
StringBuilder sb = new StringBuilder(in);
sb.reverse();
return sb.toString();
}
}
Output
Original
String: TAJMAHAL
b = b + a; // now b is sum of both the numbers
a = b - a; // b - a = (b + a) - a = b (a is swapped)
b = b - a; // (b + a) - b = a (b is swapped)
The following
example code shows one way to implement the number swap method:
public class
SwapNumbers {
public static
void main(String[] args) {
int a = 10;
int b = 20;
System.out.println("a is " + a + " and b is " + b);
a = a + b;
b = a - b;
a = a - b;
System.out.println("After swapping, a is " + a + " and b is
" + b);
}
}
The
output shows that the integer values are swapped:
Output
a
is 10 and b is 20
3-Java program to check if a vowel is present in a string
public class
StringContainsVowels {
public static
void main(String[] args) {
System.out.println(stringContainsVowels("Hello")); // true
System.out.println(stringContainsVowels("TV")); // false
}
public
static boolean stringContainsVowels(String input) {
return input.toLowerCase().matches(".*[aeiou].*");
}
}
Out
put
true
4- Java program to check if the given number is a prime number
public
class StringContainsVowels {
public static void main(String[] args) {
System.out.println(stringContainsVowels("Hello")); // true
System.out.println(stringContainsVowels("TV")); // false
}
public static boolean stringContainsVowels(String input) {
return input.toLowerCase().matches(".*[aeiou].*");
}
}
public class PrimeNumberCheck {
public static void main(String[] args) {
System.out.println(isPrime(19)); // true
System.out.println(isPrime(49)); // false
}
public static boolean isPrime(int n) {
if (n == 0 || n == 1) {
return false;
}
if (n == 2) {
return true;
}
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
Out put
true
false
5-Fibonacci series in Java
import java.util.Scanner;
public class FibonacciSeries {
public static void
main(String[] args) {
Scanner
input = new Scanner(System.in);
System.out.print("Enter the number of terms for the Fibonacci series:
");
int n =
input.nextInt();
// Initialize the
first two terms of the Fibonacci series
int firstTerm = 0;
int secondTerm = 1;
System.out.println("Fibonacci Series up to " + n + "
terms:");
// Handle the cases
for 0, 1, or more terms
if (n <= 0) {
System.out.println("Please enter a positive number of terms.");
} else if (n == 1) {
System.out.print(firstTerm);
} else {
System.out.print(firstTerm + ", " + secondTerm); // Print the first
two terms
//
Generate and print the remaining terms
for
(int i = 2; i < n; i++) {
int nextTerm = firstTerm + secondTerm;
System.out.print(", " + nextTerm);
firstTerm = secondTerm;
secondTerm = nextTerm;
}
}
System.out.println();
// New line for better formatting
input.close();
}
}
6- Remove space from a String in Java
String removeWhiteSpaces(String input)
{
StringBuilder output = new StringBuilder();
char[] charArray = input.toCharArray();
for (char c : charArray)
{
if (!Character.isWhitespace(c))
output.append(c);
}
return output.toString();
}
7-
import java.util.Scanner;
public class RemoveDuplicate {
public static String removeDuplicateChar(String
str) {
String result="";
for(int i=0;i<str.length();i++) {
int count=0;
if(str.charAt(i)!=' ') {
for(int j=i+1;j<str.length();j++) {
if(str.charAt(i)==str.charAt(j) &&(i!=j))
{
count++;
}
}
if(count==0) {
result += str.charAt(i);
}
}
}
return result;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter String: ");
String str=sc.nextLine();
System.out.println(removeDuplicateChar(str));
sc.close();
}
OUTPUT
bans
8- Program to check if a number is Even or Odd
public class CourseDetails {
public static void
main(String[] args) {
int a =
10;
if (a % 2 == 0) {
System.out.println("Given number is even");
}
else
{
System.out.println("Given number is odd");
}
}
}
public class CourseDetails {
public static void main(String[]
args) {
for (int i = 1; i
<= 100; i++) {
if (i %
2 == 1) {
System.out.println("odd " + i);
}
}
}
}
10- To check the given number is palindrome or not:
public class CourseDetails {
public static void main(String[] args) {
int num = 121;
int n=num;
int res = 0;
int rem = 0;
while (num > 0) {
rem = num % 10;
res = (res * 10) + rem;
num = num / 10;
}
if (n==res)
{
System.out.println("Given number is palindrome")
}
else
{
System.out.println("Given number is not a palindrome");
}
}
}
%10------to get the last digit from a number
*10------to increase a digit in a number
/10---------to remove the last digit from a number

No comments:
Post a Comment