You are on page 1of 5

Java programming

1.Program to find the duplicate characters in a string

public class DuplicateCharacters {

public static void main(String[] args) {

String string1 = "Great responsibility";

int count;

//Converts given string into character array

char string[] = string1.toCharArray();

System.out.println("Duplicate characters in a given string: ");

//Counts each character present in the string

for(int i = 0; i <string.length; i++) {

count = 1;

for(int j = i+1; j <string.length; j++) {

if(string[i] == string[j] && string[i] != ' ') {

count++;

//Set string[j] to 0 to avoid printing visited character

string[j] = '0';

//A character is considered as duplicate if count is greater than 1

if(count > 1 && string[i] != '0')

System.out.println(string[i]);

}
public class Example {
public static void main(String argu[]) {
String str = "beautiful beach";
char[] carray = str.toCharArray();
System.out.println("The string is:" + str);
System.out.print("Duplicate Characters in above string are:
");
for (int i = 0; i < str.length(); i++) {
for (int j = i + 1; j < str.length(); j++) {
if (carray[i] == carray[j]) {
System.out.print(carray[j] + " ");
break;
}
}
}
}
}

How to swap two numbers without


using a temporary variable?
// Java Program to swap two numbers without

// using temporary variable

import java.io.*;

class Geeks {

public static void main(String a[])

int x = 10;

int y = 5;

x = x + y;

y = x - y;
x = x - y;

System.out.println("After swaping:"

+ " x = " + x + ", y = " + y);

Prime Number-

public class Main {

public static void main(String[] args) {

int num = 29;

boolean flag = false;

for (int i = 2; i <= num / 2; ++i) {

// condition for nonprime number

if (num % i == 0) {

flag = true;

break;

if (!flag)

System.out.println(num + " is a prime number.");

else

System.out.println(num + " is not a prime number.");

Palidrome-
import java.util.Scanner;

class ChkPalindrome
{
public static void main(String args[])
{
String str, rev = "";
Scanner sc = new Scanner(System.in);

System.out.println("Enter a string:");
str = sc.nextLine();

int length = str.length();

for ( int i = length - 1; i >= 0; i-- )


rev = rev + str.charAt(i);

if (str.equals(rev))
System.out.println(str+" is a palindrome");
else
System.out.println(str+" is not a palindrome");

}
}

class PalindromeProgram
{
public static void checkPalindrome(String s)
{
// reverse the given String
String reverse = new StringBuffer(s).reverse().toString();
 
// checks whether the string is palindrome or not
if (s.equals(reverse))
System.out.println("Yes, it is a palindrome");
 
else
System.out.println("No, it is not a palindrome");
}
 
public static void main (String[] args)
throws java.lang.Exception
{
checkPalindrome("madam");
}
}
public class PalindromeProgram {
 
    public static void main(String[] args) {
 
        int rem, rev= 0, temp;
    int n=121; // user defined number to be checked for palindrome
 
        temp = n;
 
        // reversed integer is stored in variable
        while( n != 0 )
        {
            rem= n % 10;
            rev= rev * 10 + rem;
            n=n/10;
        }
 
        // palindrome if orignalInteger(temp) and reversedInteger(rev) are equal
        if (temp == rev)
            System.out.println(temp + " is a palindrome.");
        else
            System.out.println(temp + " is not a palindrome.");
    }
}

You might also like