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

Tuesday, 13 February 2018

Write a Java Program to Find Random Number

package com.tutorial4u;

import java.util.Random;
import java.util.Scanner;

public class RandomNumber {
     public static void main(String[] args)
    {
        int maxRange;
        Scanner sc = new Scanner(System.in);
        Random rand = new Random();
       
        System.out.print("Please enter maximum range: ");
        maxRange = sc.nextInt();
       
        for(int i=1; i<=10; i++)
        {
            System.out.println(rand.nextInt(maxRange));
        }
    }


}


Output:

Please enter maximum range: 500
223
263
397
375
371
468
71
124
377
374

Note : Sometimes situation arises where random numbers are required to be generated between the ranges.

package com.tutorial4u;

import java.util.Random;
import java.util.Scanner;

public class RandomNumber {
     public static void main(String[] args)
    {
           Scanner sc = new Scanner(System.in);
        System.out.print("Please enter Minimum Range : ");
        int minRange = sc.nextInt();
        System.out.print("Please enter Maximum Range : ");
        int maxRange = sc.nextInt();
        Random rand = new Random();
        int value = rand.nextInt(maxRange - minRange) + minRange;
        System.out.println(value);
    }
}


Output :

Please enter Minimum Range : 1000
Please enter Maximum Range : 5000
4304


Thursday, 1 February 2018

Print Semi Column without using semi column(;)

package com.tutorial4u;

public class SemiColonsPrint {
     public static void main(String[] args) {
              if(System.out.printf("%C",59) != null){
                  
              }         
     }

}

Output :  ;

No of Occurrence in Array

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