Friday, 30 March 2018

Find odd or even Numbers in an Array

package com.tutorial4u;

import java.util.Scanner;

/*Java Program to Find odd or even Numbers in an Array*/
public class EvenOrOddInArray {
     public static void main(String[] args) {
           Scanner sc = new Scanner(System.in);

           System.out.print("Enter the size of Array : ");
           int num = sc.nextInt();
           int arr[] = new int[num];
           System.out.println("Enter all the element : ");
           for(int i=0;i<num;i++){
                arr[i]=sc.nextInt();
           }
           for (int i = 0; i <num; i++) {
                if (arr[i] % 2 == 0)
                      System.out.println("even");
                else
                      System.out.println("odd");
           }
     }
}


Output:

Enter the size of Array : 5
Enter all the element :
22
21
33
42
24
even
odd
odd
even
even

Sunday, 25 March 2018

Count Duplicate Character

package com.tutorial4u;

import java.util.HashSet;
import java.util.Scanner;

public class DuplicateCharCount {
     public static void main(String[] args) {
           int count = 0;
           System.out.print("Enter any String : ");
           Scanner sc = new Scanner(System.in);
           String str = sc.nextLine();
           char[] ch = str.toCharArray();
           System.out.print("Duplicate character are : ");
           for(int i = 0;i<ch.length;i++){
                for(int j = i+1;j<ch.length;j++){
                      if((ch[i] == ch[j]) && (i!=j)){
                           System.out.print(ch[i]);
                           count++;
                           break;
                      }
                }
           }
     }
}



Output : 

Enter any String : tutorial4u
Duplicate character are : tu

Friday, 2 March 2018

Remove Duplicate String From Two String

package com.tutorial4u;

public class DeleteRepetedCharacterFromTwoString {

     public static void main(String[] args) {
           String s1 = "Tutorial";
        String s2 = "TotalJava";
        System.out.println("String s1 is = " + s1 + " , String s2 is = " + s2);

        char c1[] = s1.toCharArray();
        char c2[] = s2.toCharArray();
       
        for (int i = 0; i < c1.length; i++) {
          
               boolean charMatch = false;
              
               for (int j = 0; j < c2.length; j++) {
                     if ((String.valueOf(c1[i])).toLowerCase().equals(
                                   ((String.valueOf(c2[j])).toLowerCase()))) {
                       charMatch = true;
                            break;
                     }
               }
               if (charMatch) {
                     s1 = s1.replaceAll(String.valueOf(c1[i]).toUpperCase(), "");
                     s1 = s1.replaceAll(String.valueOf(c1[i]).toLowerCase(), "");
                     s2 = s2.replaceAll(String.valueOf(c1[i]).toLowerCase(), "");
                     s2 = s2.replaceAll(String.valueOf(c1[i]).toUpperCase(), "");
               }
        }
        System.out.println("After deleted s1 = " + s1 + ",  after deleted s2 = " + s2);
     }
}



Output :


String s1 is = Tutorial , String s2 is = TotalJava
After deleted s1 = uri,  after deleted s2 = Jv

No of Occurrence in Array

package com.tutorial4u; import java.util.HashMap; /************************************************************  No of Occurrence in Array...