Pages

Java Interview Programming


1. Given date in dd-MM-yyyy format.return the month in full name format(January)

Input : "23-01-2012"
Output : January
package com.jaladhi;
import java.text.SimpleDateFormat;
import java.util.*;
public class Eight {
      public static String monthDiff(Date d1){
            SimpleDateFormat sdf = new SimpleDateFormat("MMMM");
            String s = (sdf.format(d1));
            return s;
      }
      public static void main(String[] args) {
            Date d1 = new Date(23/01/2012);
            System.out.println(monthDiff(d1));
      }
}

Output :

January

2. Two dates are given as input in "yyyy-MM-dd" format. Find the number of months between the two dates.

Input 1 : "2012-12-01"
Input 2 : "2012-01-03"
Output : 11
package com.jaladhi;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Nine {
      public static int monthDiff(String s1,String s2) throws ParseException{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            Date d1 = sdf.parse(s1);
            Date d2 = sdf.parse(s2);
            c.setTime(d1);
            int m1 = c.get(Calendar.MONTH);
            int y1 = c.get(Calendar.YEAR);
            c.setTime(d2);
            int m2 = c.get(Calendar.MONTH);
            int y2 = c.get(Calendar.YEAR);
            int n = (y1-y2)*12+(m1-m2);
            return n;
      }
      public static void main(String[] args) throws ParseException {
            String s1 = new String("2013-12-01");
            String s2 = new String("2012-01-03");
            System.out.println(monthDiff(s1,s2));
      }
}

Output :

11

3. arraylist of string type which has name#mark1#mark2#mark3 format. retrieve the name of the student who has scored max marks(total of three)

Input : {"arun#12#12#12","deepak#13#12#12"}
Output : deepak
package com.jaladhi;
import java.util.*;
public class Ten {
      public static String retrieveMaxScoredStudent(String[] s1){
            Map m1 = new HashMap();
            for(int i = 0;i < s1.length;i++){
                  String s2 = s1[i];
                  StringTokenizer t = new StringTokenizer(s2,"#");
                  String s3 = t.nextToken();
                  int n1 = Integer.parseInt(t.nextToken());
                  int n2 = Integer.parseInt(t.nextToken());
                  int n3 = Integer.parseInt(t.nextToken());
                  int n = n1+n2+n3;
                  m1.put(s3, n);
            }
            //System.out.println(m1);
            int max = 0;
            String m = new String();
            Iterator i = m1.keySet().iterator();
            while(i.hasNext()){
                  String s4 = i.next();
                  int j = m1.get(s4);
                  if(j > max){
                        max = j;
                        m = s4;
                  }
            }
            return m;
      }
      public static void main(String[] args) {
            String[] s1 = {"arun#12#12#12","deepak#13#12#12","puppy#12#11#12"};
            System.out.println(retrieveMaxScoredStudent(s1));
      }
}

Output :

deepak

4.Two inputs of a string array and a string is received. The array shld be sorted in descending order. The index of second input in a array shld be retrieved.

Input 1 : {"ga","yb","awe"}
Input 2 : awe
Output : 2
package com.jaladhi;
import java.util.*;
public class Eleven {
      public static void main(String[] args) {
            String[] s1 = {"good","yMe","awe"};
            String s2 = "awe";
            System.out.println(stringRetrieval(s1,s2));
      }
      public static int stringRetrieval(String[] s1, String s2){
            ArrayList l1 = new ArrayList();
            for(int i = 0;i < s1.length;i++)
                  l1.add(s1[i]);
            Collections.sort(l1,Collections.reverseOrder()) ;
            System.out.println(l1);
            int n = l1.indexOf(s2);
            return n;
      }
}

Output :

2

5.A time input is received as stirng. Find if the hour format is in 12 hour format. the suffix am/pm is case insensitive.

Input : "09:36 am"
Output : true
package com.jaladhi;
import java.text.*;
import java.util.*;
public class Twelve {
      public static boolean hourFormat(String s) throws ParseException{
            boolean b = false;
            StringTokenizer st = new StringTokenizer(s," ");
            String s1 = st.nextToken();
            String s2 = st.nextToken();
            StringTokenizer st1 = new StringTokenizer(s1,":");
            int n1 = Integer.parseInt(st1.nextToken());
            int n2 = Integer.parseInt(st1.nextToken());
            if((s2.equalsIgnoreCase("am"))|| (s2.equalsIgnoreCase("pm")))
                  if((n1 ≤ 12) && (n2 ≤ 59))
                        b=true;
            return b;
      }
      public static void main(String[] args) throws ParseException {
            String s = "19:36 am";
            boolean b = hourFormat(s);
            if(b == true)
                  System.out.println("The time is in 12 hr format");
            else
                  System.out.println("The time is in 24 hr format");
      }
}

Output :

true

6.Retrieve the palindorme-true number set from given number limit and return the sum

Input 1 : 90 input2:120
Logic : 99+101+111
Output : 311
package com.jaladhi;
import java.util.*;
public class Thirteen {
      public static int sumOfPalindromeNos(int n1,int n2){
            List l1 = new ArrayList();
            for(int i = n1;i ≤ n2;i++){
                  int r = 0,n3 = i;
                  while(n3 != 0){
                        r = (r*10)+(n3%10);
                        n3 = n3/10;
                  }
                  if(r == i)
                        l1.add(i);
            }
            System.out.println(l1);
            int s = 0;
            for(int i = 0;i < l1.size();i++)
                  s += l1.get(i);
            return s;
      }
      public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            System.out.println("enter the range:");
            int n1 = s.nextInt();
            int n2 = s.nextInt();
            System.out.println("sum of palindrome nos.within given range is:"+sumOfPalindromeNos(n1,n2));
      }
}

Output :

311

7.find the max length-word in a given string and return the max-length word. if two words are of same length return the first occuring word of max-length.

Input : "hello how are you aaaaa"
Output : hello(length-5)
package com.jaladhi;
import java.util.*;
public class Fourteen {
      public static String lengthiestString(String s1){
            int max = 0;
            String s2 = null;
            StringTokenizer t = new StringTokenizer(s1," ");
            while(t.hasMoreTokens()){
                  s1 = t.nextToken();
                  int n = s1.length();
                  if(n > max){
                        max=n;
                        s2=s1;                   }
            }
            return s2;
      }
      public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            System.out.println("enter the String:");
            String s1 = s.nextLine();
            System.out.println("the lengthiest string is:"+lengthiestString(s1));
      }
}

Output :

hello(length-5)

8. Get a input string. reverse it and parse it with '-'.

Input : computer
Output : r-e-t-u-p-m-o-c

package com.jaladhi;
import java.util.Scanner;
public class Fifteen {
      public static String reversedAndParsedString(String s1){
            StringBuffer sb = new StringBuffer(s1);
            sb.reverse();
            StringBuffer sb1 = new StringBuffer();
            for(int i = 0;i < (2*s1.length()) - 1;i++)
                  if(i%2 != 0)
                        sb1=sb.insert(i, '-');
            return sb1.toString();
      }
      public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            System.out.println("enter the String:");
            String s1 = s.next();
            System.out.println("the formatted string is:"+reversedAndParsedString(s1));
      }
}

Output :

r-e-t-u-p-m-o-c

9. HashMap with regnum as key and mark as value. find the avg of marks whose key is odd.

Input:{12:90,35:90,33:90,56:88}
Output:avg of(90+90) =90

package com.jaladhi;
import java.util.*;
public class Sixteen {
      public static int averageOfMarks(Map m1){
            int n = 0,c = 0;
            Iterator i = m1.keySet().iterator();
            while(i.hasNext()){
                  Integer b = i.next();
                  if(b%2 != 0){
                        c++;
                        n+=m1.get(b);
                  }
            }
            return (n/c);
      }
      public static void main(String[] args) {
            Map m1 = new HashMap();
            m1.put(367, 89);
            m1.put(368, 98);
            m1.put(369, 92);
            m1.put(366, 74);
            m1.put(365, 67);
            System.out.println(averageOfMarks(m1));
      }
}

Output :

90

10. A integer input is accepted. find the square of individual digits and find their sum.

Input:125
Output:1*1+2*2+5*5

package com.jaladhi;
import java.util.*;
public class Seventeen {
      public static int averageOfMarks(Map m1){
            int n = 0,c = 0;
            Iterator i = m1.keySet().iterator();
            while(i.hasNext()){
                  Integer b = i.next();
                  if(b%2 != 0){
                        c++;
                        n += m1.get(b);
                  }
            }
            return (n/c);
      }
      public static void main(String[] args) {
            Map m1 = new HashMap();
            m1.put(367, 89);
            m1.put(368, 98);
            m1.put(369, 92);
            m1.put(366, 74);
            m1.put(365, 67);
            System.out.println(averageOfMarks(m1));
      }
}

Output :

1*1+2*2+5*5

11.Two input strings are accepted. If the two strings are of same length concat them and return, if they are not of same length, reduce the longer string to size of smaller one, and concat them

Input 1 : "hello"
Input 2 : "hi"
Output : "lohi"

Input 1 : "aaa"
Input 2 : "bbb"
Output : "aaabbb"

package com.jaladhi;
import java.util.*;
public class Eighteen {
      public static String concatStrings(String s1,String s2){
            StringBuffer sb = new StringBuffer();
            if(s1.length() == s2.length())
                  sb.append(s1).append(s2);
            else if(s1.length() > s2.length()){
                  s1 = s1.substring(s1.length()/2, s1.length());
                  sb.append(s1).append(s2);
            }
            else if(s1.length() < s2.length()){
                  s2 = s2.substring(s2.length()/2, s2.length());
                  sb.append(s1).append(s2);
            }
            return sb.toString();
      }
      public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            String s1 = s.next();
            String s2 = s.next();
            System.out.println(concatStrings(s1,s2));
      }
}

Output :

First Input : hello
Second Input : hi
Output : llohi

12.accept a string and find if it is of date format "dd/mm/yyyy".

Input : 01/13/2012
Output : false

package com.jaladhi;
import java.util.StringTokenizer;
public class Ninteen {
      public static boolean formattingDate(String s){
            boolean b1 = false;
            StringTokenizer t = new StringTokenizer(s,"/");
            int n1 = Integer.parseInt(t.nextToken());
            int n2 = Integer.parseInt(t.nextToken());
            String s2 = t.nextToken();
            int n3 = Integer.parseInt(s2);
            int n4 = s2.length();
            if(n4 == 4) {
                  if(n3%4 == 0) {
                        if((n2 == 2) && (n1 ≤ 29))
                              b1 = true;
                        else if((n2 == 4)||(n2 == 6)||(n2 == 9)||(n2 == 11) && (n1 ≤ 30))
                              b1=true;
                        else if((n2 == 1)||(n2 == 3)||(n2 == 5)||(n2 == 7)||(n2 == 8)||(n2 == 10)||(n2 == 12) && (n1 ≤ 31))
                              b1=true;
                  } else {
                        if((n2 == 2) && (n1 ≤ 28))
                              b1 = true;
                        else if((n2 == 4)||(n2 == 6)||(n2 == 9)||(n2 == 11) && (n1 ≤ 30))
                              b1 = true;
                        else if((n2 == 1)||(n2 == 3)||(n2 == 5)||(n2 == 7)||(n2 == 8)||(n2 == 10)||(n2 == 12) && (n1 ≤ 31))
                              b1 = true;
                  }
            }
            return b1;
      }
      public static void main(String[] args) {
            String s = "31/5/2012";
            boolean b = formattingDate(s);
            if(b == true)
                  System.out.println("Date is in dd/MM/yyyy format (True)");
            else
                  System.out.println("Date is not in dd/MM/yyyy format (False)");
            }
}

Output :

Date is in dd/MM/yyyy format (True).

13. Get a integer array as input. Find the average of the elements which are in the position of prime index

Input : {1,2,3,4,5,6,7,8,9,10}
Logic : 3+4+6+8(The indeces are prime numbers)
Output : 21
package com.jaladhi;
import java.util.*;
public class Twenty {
      public static int sumOfPrimeIndices(int[] a,int n){
            int n1 = 0;
            for(int i = 2;i < n;i++){
                  int k = 0;
                  for(int j = 2;j < i;j++)
                        if(i%j == 0)
                              k++;
                        if(k == 0)
                              n1 += a[i];
            }
            return n1;
      }
      public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            System.out.println("Enter the array limit:");
            int n = s.nextInt();
            System.out.println("Enter the array elements:");
            int[] a = new int[n];
            for(int i = 0;i < n;i++)
                  a[i] = s.nextInt();
            System.out.println(sumOfPrimeIndices(a,n));
      }
}

Output :

Enter the array limit: 10
Enter the array elements: 1 2 3 4 5 6 7 8 9 10
Output : 21

14.Find the extension of a given string file.

Input : "hello.jpeg"
Output : "jpeg"
package com.jaladhi;
import java.util.*;
public class TwentyOne {
      public static String extensionString(String s1){
            StringTokenizer t = new StringTokenizer(s1,".");
            t.nextToken();
            String s2 = t.nextToken();
            return s2;
      }
      public static void main(String[] args) {
            String s1 = "hello.jpeg";
            System.out.println(extensionString(s1));
      }
}

Output :

jpeg

15.Get two integer arrays as input. Find if there are common elements in the arrays. find the number of common elements.

Input 1 : {1,2,3,4}
Input 2 : {3,4,5,6}
Output : 4(3,4,3,4)
package com.jaladhi;
public class TwentyTwo {
      public static int commonElements(int[] a,int[] b){
            int c = 0;
            for(int i = 0;i < a.length;i++)
                  for(int j = 0;j < b.length;j++)
                        if(a[i] == b[j])
                              c++;
            return (2*c);
      }
      public static void main(String[] args){
            int a[] = {1,2,3,4,5};
            int b[] = {3,4,5,6,7};
            System.out.println(commonElements(a,b));
      }
}

Output :

6 {3, 4, 5}

16.Find if a given pattern appears in both the input strings at same postions.

Input 1 : "hh--ww--"
Input 2 : "rt--er--"
Output: true(false otherwise)
package com.jaladhi;
public class TwentyThree {
      public static boolean stringPattern(String s1,String s2){
            String st1 = s1.length() < s2.length()?s1:s2;
            String st2 = s1.length() > s2.length()?s1:s2;
            boolean b = true;
            String s = st2.substring(st1.length());
            if(s.contains("-"))
                  b = false;
            else {
                  loop:
                        for(int i = 0;i < st1.length();i++)
                              if((st1.charAt(i) == '-') || (st2.charAt(i) == '-'))
                                    if(st1.charAt(i) != st2.charAt(i)) {
                                          b = false;
                                          break loop;
                                    }
            }
            return b;
      }
      public static void main(String[] args) {
            String s1 = "he--ll--";
            String s2 = "we--ll--";
            boolean b = stringPattern(s1,s2);
            if(b == true)
                  System.out.println("same pattern");
            else
            System.out.println("different pattern");
      }
}

Output :

Same Pattern (True)

17. Input a int array. Square the elements in even position and cube the elements in odd position and add them as result.

Input : {1,2,3,4}
Output : 1^3+2^2+3^3+4^2
package com.jaladhi;
public class TwentyFour {
      public static int squaringAndCubingOfElements(int[] a){
            int n1 = 0,n2 = 0;
            for(int i = 0;i < a.length;i++)
                  if(i%2 == 0)
                        n1 += (a[i]*a[i]*a[i]);
                  else
                        n2 += (a[i]*a[i]);
            return (n1+n2);
      }
      public static void main(String[] args) {
            int a[] = {1,2,3,4,5,6};
            System.out.println(squaringAndCubingOfElements(a));
      }
}

Output :

209

18. Check whether a given string is palindrome also check whether it has atleast 2 different vowels.

Input : "madam"
Output : false(no 2 diff vowels)
package com.jaladhi;
public class TwentyFive {
      public static boolean palindromeAndVowelCheck(String s){
            boolean b = true;
            int c = 0;
            String s1 = "aeiou";
            StringBuffer sb = new StringBuffer(s);
            String s2 = sb.reverse().toString();
            if(!s2.equals(s))
                  b=false;
            else{
                  loop:
                        for(int i = 0;i < s1.length();i++)
                              for(int j = 0;j < s.length();j++)
                                    if(s1.charAt(i) == s.charAt(j)){
                                          c++;
                                    continue loop;
                                    }
                        if(c < 2)
                              b = false;
                        }
            return b;
      }
      public static void main(String[] args) {
            String s = "deed";
            System.out.println(palindromeAndVowelCheck(s));
      }
}

Output :

False

19. Find no of characters in a given string which are not repeated.

Input : "hello"
Output : 3
package com.jaladhi;
public class TwentySix {
      public static int noOfnonRepeatedCharacters(String s1){
            StringBuffer sb = new StringBuffer(s1);
            for(int i=0; i < sb.length();i++){
                  int n=0;
                  for(int j = i+1;j < sb.length();j++)
                        if(sb.charAt(i) == sb.charAt(j)){
                              sb.deleteCharAt(j);
                        n++;
                  }
                  if(n > 0){
                        sb.deleteCharAt(i);
                        i--;
                  }
            }
            return sb.length();
      }
      public static void main(String[] args) {
            String s1 = "preethi";
            System.out.println(noOfnonRepeatedCharacters(s1));
      }
}

Output :

5

20. Get a input string. Find if it is a negative number, if true return the absolute value, in other cases return -1.

Input : "-123"
Output : 123
Input : "@123"
Output : -1
package com.jaladhi;
import java.util.*;
public class TwentySeven {
      public static int negativeString(String s1){
            int n1 = 0;
            if(s1.startsWith("-")){
                  int n=Integer.parseInt(s1);
                  if(n < 0)
                        n1 = Math.abs(n);}
                  else
                        n1 = -1;
            return n1;
      }
      public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            String s1 = s.next();
            System.out.println(negativeString(s1));
      }
}

Output :

Input : -123
Output : 123

21.An arraylist of Strings is given as input. The count of the String elements that are not of size input2 string is to be returned.

Input 1 : {"aaa","bb","cccc","d"}
Input 2 : "bb"
Output : 3("bb"-length:2)
package com.jaladhi;
import java.util.*;
public class TwentyEight {
      public static int StringsNotOfGivenLength(List l1,String s1){
            int n1 = s1.length();
            int c = 0;
            for(int i = 0;i < l1.size();i++){
                  int n2 = l1.get(i).length();
                  if(n1 != n2)
                        c++;
                  }
            return c;
      }
      public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            System.out.println("Enter the no.of elements : ");
            int n = s.nextInt();
            List l1 = new ArrayList();
            for(int i = 0;i < n;i++)
                  l1.add(s.next());
            System.out.println("Enter the input string : ");
            String s1 = s.next();
            System.out.println(StringsNotOfGivenLength(l1,s1));
      }
}

Output :

Enter the no. of elements : 3
aaa bb cccc d
Enter the Input String : bb
2

22.An array of integers is given.The given element is removed new array is returned. Input 1 : {10,10,20,30,76}
Output : {20,20,76}(10 is asked to remove)

package com.jaladhi;
import java.util.*;
public class TwentyNine {
      public static int[] removalOfGivenElementFromArray(int[] a,int c){
            List l1 = new ArrayList();
            for(int i = 0;i < a.length;i++)
                  if(a[i] != c)
                        l1.add(a[i]);
            int b[] = new int[l1.size()];
            for(int i = 0;i < b.length;i++)
                  b[i] = l1.get(i);
            return b;
      }
      public static void main(String[] args) {
            int a[] = {10,10,20,30,40,50};
            int c = 10;
            int[] b = removalOfGivenElementFromArray(a,c);
      for(int d:b)
            System.out.println(d);
      }
}

Output :

20 30 40 50

23. Find the number of days between two input dates.

package com.jaladhi;
import java.text.*;
import java.util.*;
public class Thirty {
      public static int dateDifference(String s1,String s2) throws ParseException{
            SimpleDateFormat sd = new SimpleDateFormat("dd/MM/yyyy");
            Date d = sd.parse(s1);
            Calendar c = Calendar.getInstance();
            c.setTime(d);
            long d1 = c.getTimeInMillis();
            d=sd.parse(s2);
            c.setTime(d);
            long d2 = c.getTimeInMillis();
            int n = Math.abs((int) ((d1-d2)/(1000*3600*24)));
            return n;
      }
      public static void main(String[] args) throws ParseException {
            String s1 = "27/12/2009";
            String s2 = "15/09/2012";
            System.out.println(dateDiffer ence(s1,s2));
      }
}

Output :

993

24.Enter yout name and return a string "print the title first and then comma and then first letter of initial name.

package com.jaladhi;
import java.util.*;
public class ThirtyOne {
      public static String retrieveName(String s1){
            StringTokenizer t = new StringTokenizer(s1," ");
            String s2 = t.nextToken();
            String s3 = t.nextToken();
            StringBuffer sb = new StringBuffer(s2);
            sb.append(',').append(s3.charAt(0));
            return sb.toString();
      }
      public static void main(String[] args) {
            String s1 = "Bhavane Raghupathi";
            System.out.println(retrieveName(s1));
      }
}

Output :

Bhavane,R

25.Write a Program that accepts a string and removes the duplicate characters.

package com.jaladhi;
public class ThirtyTwo {
      public static String removalOfDuplicateCharacters(String s1){
            StringBuffer sb = new StringBuffer(s1);
            for(int i = 0;i < s1.length();i++)
                  for(int j = i+1;j < s1.length();j++)
                        if(s1.charAt(i) == s1.charAt(j))
                              sb.deleteCharAt(j);
            return sb.toString();
      }
      public static void main(String[] args) {
            String s1 = "bhavane";
            System.out.println(removalOfDuplicateCharacters(s1));
      }
}

Output :

bhavne

26.Validate a Password
i) There should be atleast one digit.
ii) There should be atleast one of '#','@' or '$'.
iii) The password should be of 6 to 20 characters.
iv) There should be more uppercase letter than lower case.
v) Should start with an upper case and end with lower case.

package com.jaladhi;
import java.util.*;
public class ThirtyThree {
      public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            String st = s.next();
            boolean b = validatingPassword(st);
            if(b == true)
                  System.out.println("valid password");
            else
                  System.out.println("Invalid Password");
      }
      public static boolean validatingPassword(String st) {
            boolean b1 = false,b2 = false;
            if(Character.isUpperCase(st.charAt(0)))
                  if(Character.isLowerCase(st.charAt(st.length()-1)))
                        if(st.length() >= 6 && st.length() ≤ 20)
                              for (int i = 0; i < st.length();i++) {
                                    char c = st.charAt(i);
                                    if(Character.isDigit(c)){
                                          b1 = true; break;
                                    }
      }
      int x = 0,y = 0;
      for(int i = 0; i < st.length(); i++)
            if(Character.isUpperCase(st.charAt(i)))
                  x++;
            else if(Character.isLowerCase(st.charAt(i)))
                  y++;
            if(b1 == true)
                  if(x > y)
                        for (int i = 0; i < st.length();i++) {
                              char c = st.charAt(i);
                              if(c == '#' || c == '@' || c == '$'){
                                    b2 = true; break;
                              }
                        }
            return b2;
      }
}

Output :

Input : nagavijay.j@outlook.com
Output : Invalid Password

27. find the average of the maximum and minimum number in an integer array.

package com.jaladhi;
import java.util.*;
public class ThirtyFour {
      public static void main(String[] args) {
            int[] a = {10,54,23,14,32,45};
            System.out.println(avgOfMaxandMinNoinArray(a));
      }
      public static int avgOfMaxandMinNoinArray(int[] a) {
            Arrays.sort(a);
            int b = a[0];
            int c = a[a.length-1];
      return (b+c)/2;
      }
}

Output :

32

28.validate the ip address in the form a.b.c.d where a,b,c,d must be between 0and 255 if validated return 1 else return 2.

package com.jaladhi;
import java.util.*;
public class ThirtyFive {
      public static void main(String[] args) {
            String ipAddress = "010.230.110.160";
            boolean b=validateIpAddress(ipAddress);
            if(b == true)
                  System.out.println("valid ipAddress");
            else
                  System.out.println("not a valid ipAddress");
      }
      public static boolean validateIpAddress(String ipAddress) {
            boolean b1 = false;
            StringTokenizer t = new StringTokenizer(ipAddress,".");
            int a = Integer.parseInt(t.nextToken());
            int b = Integer.parseInt(t.nextToken());
            int c = Integer.parseInt(t.nextToken());
            int d = Integer.parseInt(t.nextToken());
            if((a >= 0 && a ≤ 255) && (b >= 0 && b ≤ 255) && (c >= 0 && c ≤ 255) && (d >= 0 & d ≤ 255))
                  b1=true;
            return b1;
      }
}

Output :

valid IP Address

29. find the three consecutive characters in a string. if the string consists any three consecutive characters return 1 else return -1 like AAAxyzaaa will return true.

package com.jaladhi;
public class ThirtySix {
      public static void main(String[] args) {
            String s1 = "aaaaxyzAAA";
            boolean b = consecutiveCharactersCheck(s1);
            if(b == true)
                  System.out.println("presence of consecutive characters");
            else
                  System.out.println("absence of consecutive characters");
      }
      public static boolean consecutiveCharactersCheck(String s1) {
            boolean b=false;
            int c=0;
            for(int i=0;i < s1.length()-1;i++)
                  if((s1.charAt(i+1)-s1.charAt(i))==1)
                        c++;
                  if(c>=2)
                        b=true;
            return b;
      }
}

Output :

Presence of Consecutive Characters

30.String encrption. replace the odd-index character with next character(if it is 'z' replace with 'a'..if 'a' with 'b' as such), leave the even index as such. return the encrypted string.

package com.jaladhi;
public class ThirtySeven {
      public static void main(String[] args) {
            String s = "preethi";
            System.out.println(oddEncryptionOfString(s));
      }
      public static String oddEncryptionOfString(String s) {
            StringBuffer sb = new StringBuffer();
            for(int i = 0;i < s.length();i++){
                  char c = s.charAt(i);
                  if(c%2 != 0){
                        c = (char)(c+1);
                        sb.append(c);
                  }
                  else
                        sb.append(c);
            }
      return sb.toString();
      }
}

Output :

prffthj

31. Retrieve the max from array which is in a odd-index.

package com.jaladhi;
import java.util.*;
public class ThirtyEight {
      public static void main(String[] args) {
            int[] a = {10,23,45,54,32,14,55,65,56};
            System.out.println(retrieveMaxFromOddIndex(a));
      }
      public static int retrieveMaxFromOddIndex(int[] a) {
            List l = new ArrayList();
            for(int i = 0;i < a.length;i++)
                  if(i%2 != 0)
                        l.add(a[i]);
            int b=0;
            for(int i = 0;i < l.size();i++){
                  int c = (Integer) l.get(i);
                  if(c > b)
                        b = c;
            }
            return b;
      }
}

Output :

65

32.GIVEN A STRING 555-666-1234. DISPLAY AS 55-56-661-234?

package com.jaladhi;
import java.util.StringTokenizer;
public class ThirtyNine {
      public static void main(String[] args) {
            String s = "555-666-1234";
            System.out.println(display(s));
      }
      public static String display(String s) {
            StringTokenizer t = new StringTokenizer(s,"-");
            String s1 = t.nextToken();
            String s2 = t.nextToken();
            String s3 = t.nextToken();
            StringBuffer sb = new StringBuffer();
            sb.append(s1.substring(0, s1.length()-1)).append('-');
            sb.append(s1.charAt(s1.length()-1)).append(s2.charAt(0)).append('-');
            sb.append(s2.substring(1, s2.length())).append(s3.charAt(0)).append('-');
            sb.append(s3.substring(1, s3.length()));
            return sb.toString();
      }
}

Output :

55-56-661-234

33. Input 1 = "Rajasthan";
Input 2 = 2;
Input 3 = 5;
Output = hts;

package com.jaladhi;
public class Fourty {
      public static void main(String[] args) {
            String input1 = "Rajasthan";
            int input2 = 2, input3 = 5;
            System.out.println(retrieveString(input1,input2,input3));
      }
      public static String retrieveString(String input1, int input2, int input3) {
            StringBuffer sb = new StringBuffer(input1);
            sb.reverse();
            String output = sb.substring(input2, input3);
            return output;
      }
}

Output :

hts

34. Input 1 = {1,2,3}
Input 2 = {3,4,5}
Input 3; + (union)
Output : Input 1 + Input 2

Input 1 : {1,2,3,4,5}
Input 2 : {2,3,4,5}
Input 3 = *(intersection)
Output : Input 1 * Input 2

INPUT 1 : {1,2,3,4,5}
INPUT 2 : {3,6,7,8,9}
INPUT 3 : - (MINUS)
OUTPUT : INPUT 1 - INPUT 2

package com.jaladhi;
import java.util.*;
public class FourtyOne {
      public static void main(String[] args) {
            int[] a = {1,2,3,4,5};
            int[] b = {0,2,4,6,8};
            Scanner s = new Scanner(System.in);
            int c = s.nextInt();
            int d[] = arrayFunctions(a,b,c);
            for(int e:d)
                  System.out.println(e);
      }
      public static int[] arrayFunctions(int[] a, int[] b, int c) {
            List l1 = new ArrayList();
            List l2 = new ArrayList();
            List l3 = new ArrayList();
            for(int i = 0;i < a.length;i++)
                  l1.add(a[i]);
            for(int i = 0;i < b.length;i++)
                  l2.add(b[i]);
            switch(c){
                  case 1:
                        l1.removeAll(l2);
                        l1.addAll(l2);
                        Collections.sort(l1);
                        l3 = l1;
                        break;
                  case 2:
                        l1.retainAll(l2);
                        Collections.sort(l1);
                        l3 = l1;
                        break;
                  case 3:
                        if(l1.size() == l2.size())
                              for(int i = 0;i < l1.size();i++)
                                    l3.add(Math.abs(l2.get(i)-l1.get(i)));
                        break;
            }
            int[] d = new int[l3.size()];
            for(int i = 0;i < d.length;i++)
                  d[i] = l3.get(i);
            return d;
      }
}

Output :

Input : 2
Output : {2, 4}

35. Input 1 = commitment;
Output = cmmitmnt;
c be the first index position remove even vowels from the string.

package com.jaladhi;
public class FourtyTwo {
      public static void main(String[] args) {
            String s1 = "cOmmitment";
            System.out.println(removeEvenElements(s1));
      }
      public static String removeEvenElements(String s1) {
            StringBuffer sb1 = new StringBuffer();
            for(int i = 0;i < s1.length();i++)
                  if((i%2) == 0)
                        sb1.append(s1.charAt(i));
                  else if((i%2) != 0)
                        if(s1.charAt(i) != 'a' && s1.charAt(i) != 'e' && s1.charAt(i) != 'i' && s1.charAt(i) != 'o' && s1.charAt(i) != 'u')
                              if(s1.charAt(i) != 'A' && s1.charAt(i) != 'E' && s1.charAt(i) != 'I' && s1.charAt(i) != 'O' && s1.charAt(i) != 'U')
                                    sb1.append(s1.charAt(i));
            return sb1.toString();
      }
}

Output :

cmmitmnt

36. Retrieve the odd-position digits from input integer array. Multiply them with their index and return their sum.

package com.jaladhi;
public class FourtyThree {
      public static void main(String[] args) {
            int[] a = {5,1,6,2,9,4,3,7,8};
            System.out.println("Sum Of Odd Position Elements Multiplied With Their Index Is:");
            System.out.println(retrievalOfOddPositionElements(a));
      }
      public static int retrievalOfOddPositionElements(int[] a) {
            int s = 0;
            for(int i = 0;i < a.length;i++)
                  if(i%2 != 0)
                        s += a[i]*i;
            return s;
      }
}

Output :

Sum Of Odd Position Elements Multiplied With Their Index Is: 76

37. Return the number of days in a month, where month and year are given as input.

package com.jaladhi;
import java.util.*;
public class FourtyFour {
      public static void main(String[] args){
            String s1 = "02/2011";
            System.out.println(noOfDaysInAmonth(s1));
      }
      public static int noOfDaysInAmonth(String s1){
            int n = 0;
            StringTokenizer r = new StringTokenizer(s1,"/");
            int n1 = Integer.parseInt(r.nextToken());
            int n2 = Integer.parseInt(r.nextToken());
            if(n1 == 1 || n1 == 3 || n1 == 5 ||n1 == 7 || n1 == 8 || n1 == 10 || n1 == 12)
                  n = 31;
            else if(n1 == 4 || n1 == 6 || n1 == 9 || n1 == 11)
                  n = 30;
            else if(n1 == 2){
                  if(n2%4 == 0)
                        n=29;
                  else
                        n=28;
            }
            return n;
      }
}

Output :

28

38. Retrieve the non-prime numbers within the range of a given input. Add-up the non-prime numbers and return the result.

package com.jaladhi;
import java.util.*;
public class FourtyFive {
      public static void main(String[] args) {
            int i = 20,j = 40;
            System.out.println("sum of non-prime nos. is:"+additionOfnonPrimeNos(i,j));
      }
      public static int additionOfnonPrimeNos(int i, int j){
            int k = 0;
            List < Integer > l1 = new ArrayList < Integer > ();
            List < Integer > l2 = new ArrayList < Integer > ();
            for(int a = i;a ≤ j;a++){
                  int c=0;
                  for(int b = 2;b < a;b++)
                        if(a%b == 0)
                              c++;
                  if(c == 0)
                        l1.add(a);
            }
            for(int e = i;e ≤ j;e++)
                  l2.add(e);
            l2.removeAll(l1);
            for(int d=0;d < l2.size();d++)
                  k += l2.get(d);
            return k;
      }
}

Output :

sum of non-prime nos. is:510

39. Retrieve the elements in a array, which is an input, which are greater than a specific input number. Add them and find the reverse of the sum.

package com.jaladhi;
public class FourtySix {
      public static void main(String[] args) {
            int[] a = {23,35,11,66,14,32,65,56,55,99};
            int b = 37;
            System.out.println(additionOfRetrievedElements(a,b));
      }
      public static int additionOfRetrievedElements(int[] a, int b) {
            int n = 0;
            for(int i = 0;i < a.length;i++)
            if(a[i] > b)
                  n += a[i];
            return n;
      }
}

Output :

341

40.Input a HashMap. Count the keys which are not divisible by 4 and return.

package com.jaladhi;
import java.util.*;
public class FourtySeven {
      public static void main(String[] args) {
            Map < Integer, String > m1 = new HashMap < Integer, String > ();
            m1.put(24, "preethi");
            m1.put(32, "bhanu");
            m1.put(25, "menu");
            m1.put(31, "priya");
            System.out.println(fetchingKeysDivisibleByFour(m1));
      }
      public static int fetchingKeysDivisibleByFour(Map < Integer, String > m1) {
            int k = 0;
            Iterator i = m1.keySet().iterator();
            loop:
            while(i.hasNext()){
                  int j = Integer.parseInt(i.next().toString());
                  if(j%4 != 0)
                        k++;
            continue loop;
            }
      return k;
      }
}

Output :

2

41. compare two strings, if the characters in string 1 are present in string 2, then it should be put as such in output, else '+' should be put in output...ignore case difference. Input 1 : "New York"
Input 2 : "NWYR"
Output : N+w+Y+r+

package com.jaladhi;
public class FourtyEight {
      public static void main(String[] args) {
            String s1 = "New York";
            String s2 = "NWYR";
            System.out.println(StringFormatting(s1,s2));
      }
      public static String StringFormatting(String s1, String s2) {
            StringBuffer s4 = new StringBuffer();
            String s3 = s1.toUpperCase();
            for(int i = 0;i < s2.length();i++)
                  for(int j = 0;j < s3.length();j++)
                        if(s2.charAt(i) == s3.charAt(j))
                              s4.append(s1.charAt(j)).append('+');
            return s4.toString();
      }
}

Output :

N+w+Y+r+

42. Input :
Searchstring s1 = "GeniusRajkumarDev";
String s2 = "Raj";
String s3 = "Dev";
Output:
Return 1 if s2 comes before s3 in searchstring else return 2

package com.jaladhi;
public class FourtyNine {
      public static void main(String[] args) {
            String srch = "MaanVeerSinghKhurana";
            String s1 = "Veer";
            String s2 = "Singh";
            int n = searchString(srch,s1,s2);
            if(n == 1)
                  System.out.println(s1 + " comes before "+s2);
            else
                  System.out.println(s2 + " comes before "+s1);
      }
      public static int searchString(String srch, String s1, String s2) {
            int n = 0;
            int n1 = srch.indexOf(s1);
            int n2 = srch.indexOf(s2);
            if(n1 < n2)
                  n = 1;
            else
                  n = 2;
            return n;
      }
}

Output :

Veer comes before Singh

43. Months between two dates.

package com.jaladhi;
import java.text.*;
import java.util.*;
public class Fifty {
      public static void main(String[] args) throws ParseException {
            String s1 = "30/05/2013";
            String s2 = "01/06/2013";
            System.out.println(monthsBetweenDates(s1,s2));
      }
      public static int monthsBetweenDates(String s1, String s2) throws ParseException {
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            Date d1 = sdf.parse(s1);
            Date d2 = sdf.parse(s2);
            Calendar cal = Calendar.getInstance();
            cal.setTime(d1);
            int months1 = cal.get(Calendar.MONTH);
            int year1 = cal.get(Calendar.YEAR);
            cal.setTime(d2);
            int months2 = cal.get(Calendar.MONTH);
            int year2 = cal.get(Calendar.YEAR);
            int n = ((year2-year1)*12)+(months2-months1);
            return n;
      }
}

Output :

1

44. Input 1 = "xyzwabcd"
Input 2 = 2;
Input 3 = 4;
Output = bawz

package com.jaladhi;
public class FiftyOne {
      public static void main(String[] args) {
            String s1 = "xyzwabcd";
            int n1 = 2, n2 = 4;
            System.out.println(retrievalOfString(s1,n1,n2));
      }
      public static String retrievalOfString(String s1, int n1, int n2) {
            StringBuffer sb = new StringBuffer(s1);
            sb.reverse();
            String s2 = sb.substring(n1, n1+n2);
            return s2;
      }
}

Output :

bawz

45. Given integer array
Input : int[] arr = {2,3,5,4,1,6,7,7,9};
Remove the duplicate elements and print sum of even numbers in the array. print -1 if arr contains only odd numbers.

package com.jaladhi;
import java.util.*;
public class FiftyTwo {
      public static void main(String[] args) {
            int a[] = {2,3,5,4,1,6,7,7,9};
            System.out.println(sumOfEvenNos(a));
      }
      public static int sumOfEvenNos(int[] a) {
            List < Integer > l1 = new ArrayList < Integer > ();
            for(int i = 0;i < a.length;i++)
                  l1.add(a[i]);
            List < Integer > l2 = new ArrayList < Integer > ();
            for(int i = 0;i < a.length;i++)
                  for(int j = i+1;j < a.length;j++)
                        if(a[i] == a[j])
                              l2.add(a[j]);
            l1.removeAll(l2);
            l1.addAll(l2);
            int n = 0,n1;
            for(int i = 0;i < l1.size();i++)
                  if(l1.get(i)%2 == 0)
                        n += l1.get(i);
            if(n == 0)
                  n1 = -1;
            else
                  n1 = n;
            return n1;
      }
}

Output :

12

46. Input 1 = "abc2012345"
Input 2 = "abc2112660"
Input 3 = 4
Here "abc**" refers to customer id. 12345 refers to last month eb reading and 12660 refers to this month eb reading. Find the difference between two readings and multiply it by input 3 ie., output=(12660-12345)*4

package com.jaladhi;
public class FiftyThree {
      public static void main(String[] args) {
            String input1 = "abc2012345";
            String input2 = "abc2112660";
            int input3 = 4;
            System.out.println(meterReading(input1,input2,input3));
      }
      public static int meterReading(String input1, String input2, int input3) {
            int n1 = Integer.parseInt(input1.substring(5, input1.length()));
            int n2 = Integer.parseInt(input2.substring(5, input2.length()));
            int n = Math.abs((n2-n1)*input3);
            return n;
      }
}

Output :

1260

47. Given array of intergers,print the sum of elements sqaring/cubing as per the power of their indices.
//answer= sum=sum+a[i]^i;
Example : Input : {2,3,5}
Output : 29

package com.jaladhi;
public class FiftyFour {
      public static void main(String[] args) {
            int a[] = {2,3,5};
            System.out.println(sumOfElementsWrtIndices(a));
      }
      public static int sumOfElementsWrtIndices(int[] a) {
            int s = 0;
            for(int i = 0;i < a.length;i++)
                  s += (Math.pow(a[i], i));
            return s;
      }
}

Output :

29

48. Given array of string check whether all the elements contains only digits not any alaphabets.If condn satisfied print 1 else -1.
Input : {"123","23.14","522"}
Output : 1
Input 1 : {"asd","123","42.20"}
Output : -1

package com.jaladhi;
public class FiftySix {
      public static void main(String[] args) {
            String[] input1 = {"123","23.14","522"};
            //String[] input1 = {"asd","123","42.20"};
            System.out.println(stringOfDigits(input1));
      }
      public static int stringOfDigits(String[] input1) {
            int n = 0;
            for(int i = 0;i < input1.length;i++){
                  String s1 = input1[i];
                  for(int j = 0;j < s1.length();j++){
                        char c1 = s1.charAt(j);
                        if(Character.isDigit(c1))
                              n = 1;
                        else {
                              n = -1;
                              break;
                        }
                  }
            }
            return n;
      }
}

Output :

1

49. int[] a = {12,14,2,26,35}. Find the difference between max and min values in array. Output : 35 - 2 = 33.

package com.jaladhi;
import java.util.Arrays;
public class FiftySeven {
      public static void main(String[] args) {
            int a[] = {12,14,2,26,35};
            System.out.println(diffBwMaxAndMin(a));
      }
      public static int diffBwMaxAndMin(int[] a) {
            Arrays.sort(a);
            int n = a[a.length-1]-a[0];
            return n;
      }
}

Output :

33

50. Given an array find the largest 'span'(span is the number of elements between two same digits). Find their sum. a[]={1,4,2,1,4,1,5} Largest span = 5

package com.jaladhi;
public class Sixty {
      public static void main(String[] args) {
            int a[] = {1,4,2,1,4,1,5};
            System.out.println("sum of largest span elements:"+largestSpan(a));
      }
      public static int largestSpan(int[] a) {
            int max = 0;
            int p1 = 0;
            int p2 = 0;
            int n = 0;
            int sum = 0;
            for(int i = 0;i < a.length-1;i++){
                  for(int j = i+1;j < a.length;j++)
                        if(a[i] == a[j])
                              n = j;
                        if(n-i > max){
                              max = n-i;
                              p1 = i;
                              p2 = n;
                        }
                  }
                  System.out.println("largest span:"+(p2-p1));
                  for(int i = p1;i ≤ p2;i++)
                        sum = sum + a[i];
            return (sum);
      }
}

Output :

Largest Span = 5
Sum of Largest Span Elements = 13

51. Input = {"1","2","3","4"}
Output = 10
That is, if string elements are nos,add it.
Input = {"a","b"}
Output = -1;

package com.jaladhi;
public class SixtyOne {
      public static void main(String[] args) {
            String s[] = {"1","2","3","4"};
            //String s[] = {"a","b","3","4"};
            System.out.println(checkForStringElements(s));
      }
      public static int checkForStringElements(String[] s) {
            int n = 0;
            boolean b = false;
            for(int i = 0;i < s.length;i++){
                  String s1 = s[i];
                  for(int j = 0;j < s1.length();j++){
                        char c = s1.charAt(j);
                        if(Character.isDigit(c))
                              b = true;
                        else{
                              b = false;
                              break;
                        }
                  }
                  if(b == true)
                        n += Integer.parseInt(s1);
                  else {
                        n = -1;
                        break;
                  }
            }
            return n;
      }
}

Output :

10

52. arraylist 1 = {1,2,3,4,5}
arraylist 2 = {6,7,8,9,10}
size of both list should be same
Output={6,2,8,4,10}

package com.jaladhi;
public class SixtyTwo {
      public static void main(String[] args) {
            int a[] = {1,2,3,4,5};
            int b[] = {6,7,8,9,10};
            int c[] = alternativeIndicesElements(a,b);
            for(int d:c)
                  System.out.println(d);
      }
      public static int[] alternativeIndicesElements(int[] a, int[] b){
            int c[] = new int[a.length];
            if(a.length == b.length)
                  for(int i = 0;i < a.length;i++)
                        if(i%2 == 0)
                              c[i] = b[i];
                        else
                              c[i] = a[i];
            return c;
      }
}

Output :

6 2 8 4 10

53. count the number of times the second word in second string occurs in first string-case sensitive. Input 1 = hai hello hai where hai Hai;
Input 2 = what hai
Output = 3;

package com.jaladhi;
import java.util.StringTokenizer;
public class SixtyThree {
      public static void main(String[] args) {
            String input1 = "hai hello how are you?? hai hai";
            String input2 = "what hai";
            System.out.println(stringOccurance(input1,input2));
      }
      public static int stringOccurance(String input1, String input2){
            int count = 0;
            StringTokenizer t1 = new StringTokenizer(input2," ");
            String s1 = t1.nextToken();
            String s2 = t1.nextToken();
            StringTokenizer t2 = new StringTokenizer(input1," ");
            while(t2.hasMoreTokens()){
                  if(t2.nextToken().equals(s2))
                  count++;
            }
            return count;
      }
}

Output :

3

54. Find the number of charcters,that has repeated 3 consecutive times. Input 1 : "aaabbccc"
Ouput 1 : 2;

package com.jaladhi;
public class SixtyFour {
      public static void main(String[] args) {
            String input1 = "aaabbccc";
            System.out.println(consecutiveRepeatitionOfChar(input1));
      }
      public static int consecutiveRepeatitionOfChar(String input1) {
            int c = 0;
            int n = 0;
            for(int i = 0;i < input1.length()-1;i++) {
                  if(input1.charAt(i) == input1.charAt(i+1))
                        n++;
                  else
                        n = 0;
                  if(n == 2)
                        c++;
            }
            return c;
      }
}

Output :

2

55. What will be the DAY of current date in next year.

package com.jaladhi;
import java.text.SimpleDateFormat;
import java.util.*;
public class SixtyFive {
      public static void main(String[] args) {
            Date d = new Date();
            System.out.println(sameDayOnUpcomingYear(d));
      }
      public static String sameDayOnUpcomingYear(Date d) {
            Date d1 = new Date();
            d1.setYear(d.getYear()+1);
            SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
            String s = sdf.format(d1);
            return s;
      }
}

Output :

Thursday

56. Get all the numbers alone from the string and return the sum. Input : "123gif"
Output : 6

package com.jaladhi;
public class SixtySix {
      public static void main(String[] args) {
            String s = "1234gif";
            System.out.println(summationOfNosInaString(s));
      }
      public static int summationOfNosInaString(String s) {
            int n = 0;
            for(int i = 0;i < s.length();i++){
                  char c = s.charAt(i);
                  if(Character.isDigit(c)){
                        String s1 = String.valueOf(c);
                        n += Integer.parseInt(s1);
                  }
            }
            return n;
      }
}

Output :

6

57. Input array = {red, green, blue, ivory}
Sort the given array reverse the given array if user input is 1 it should give other element of an reversed array.

package com.jaladhi;
import java.util.*;
public class SixtySeven {
      public static void main(String[] args) {
            String[] s = {"red","green","blue","ivory","yellow"};
            int n = 1;
            System.out.println(retrievingRequiredColor(s,n));
      }
      public static String retrievingRequiredColor(String[] s, int n) {
            String s1 = new String();
            List < String > l = new ArrayList < String > ();
            for(int i = 0;i < s.length;i++)
                  l.add(s[i]);
            Collections.sort(l,Collections.reverseOrder());
            for(int i = 0;i < l.size();i++)
                  if(i == (n-1))
                        s1 = l.get(i);
            return s1;
      }
}

Output :

yellow

58. String a = "a very fine day"
Output : A Very Fine Day

package com.jaladhi;
import java.util.StringTokenizer;
public class SixtyEight {
      public static void main(String[] args){
            String s1 = "its a very fine day";
            System.out.println(capsStart(s1));
      }
      public static String capsStart(String s1){
            StringBuffer s5 = new StringBuffer();
            StringTokenizer t = new StringTokenizer(s1," ");
            while(t.hasMoreTokens()){
                  String s2 = t.nextToken();
                  String s3 = s2.substring(0,1);
                  String s4 = s2.substring(1, s2.length());
                  s5.append(s3.toUpperCase()).append(s4).append(" ");
            }
            return s5.toString();
      }
}

Output :

Its A Very Fine Day

59. Take the word with a max length in the given sentance in that check for vowels if so count the no.of occurances !

Input 1 = "Bhavane is a good girl"
Output = 3
Input 1 = "Bhavanee is a good girl"
Output = 4
package com.jaladhi;
import java.util.StringTokenizer;
public class SixtyNine {
      public static void main(String[] args) {
            String s1 = "Bhavane is a good girl";
            System.out.println(countVowelsInMaxLengthString(s1));
      }
      public static int countVowelsInMaxLengthString(String s1) {
            int n1 = 0, max = 0;
            String s4 = "AEIOUaeiou";
            String s3 = new String();
            StringTokenizer t = new StringTokenizer(s1," ");
            while(t.hasMoreTokens()){
                  String s2 = t.nextToken();
                  int n2 = s2.length();
                  if(n2 > max){
                        max = n2;
                        s3 = s2;
                  }
            }
            for(int i = 0;i < s3.length();i++)
                  for(int j = 0;j < s4.length();j++)
                        if(s3.charAt(i) == s4.charAt(j))
                              n1++;
            return n1;
      }
}

Output :

3

60. Bubble Sort Program

package com.jaladhi;
import java.util.Scanner;
public class OneHundredTwentyFive {
      public static void main(String[] args) {
            int i, j, temp;
            for(i = 0; i < n-1; i++) {
                  for(j = 0; j < n; j++) {
                        if(a[i] > a[j]) {
                              temp = a[i];
                              a[i] = a[j];
                              a[j] = temp;
                        }
                  }
            }
      }
}

Output :

Bubble Sort : 4

61. Square Root Program

package com.jaladhi;
import java.util.Scanner;
public class OneHundredTwentyFour {
      public static void main(String[] args) {
            int n = 16;
            double t;
            double sr = n/2;
            do {
                  t = sr;
                  sr = (t + (n/t))/2;
            } while((t-sr) != 0);
            System.out.println("Square Root Value : " + sr);
      }
}

Output :

Square Root Value : 4

62. Getting the first and last n letters from a word where wordlength > 2n.
Ex: Input: california,3.
output: calnia.

package com.jaladhi;
public class OneHundredSixteen {
      public static void main(String[] args) {
            String s1 = "california";
            int n1 = 3;
            System.out.println(subStringOfgivenString(s1,n1));
      }
      public static String subStringOfgivenString(String s1, int n1) {
            StringBuffer sb = new StringBuffer();
            sb.append(s1.substring(0, n1)).append(s1.substring(s1.length()-n1,s1.length()));
            return sb.toString();
      }
}

Output :

calnia

63. Input 1 = "aBrd";
Input 2 = "aqrbA";
Input3 = 2;
Output 1 = boolean true;
2nd char of ip1 and last 2nd char of ip2 show be equal.

package com.jaladhi;
public class OneHundredSeventeen {
      public static void main(String[] args) {
            String ip1 = "aBrd";
            String ip2 = "aqrbA";
            int ip3 = 2;
            System.out.println(charCheck(ip1,ip2,ip3));
      }
      public static boolean charCheck(String ip1, String ip2, int ip3){
            boolean b = false;
            String s1 = String.valueOf(ip1.charAt(ip3-1));
            String s2 = String.valueOf(ip2.charAt(ip2.length()-ip3));
            if(s1.equalsIgnoreCase(s2))
                  b = true;
            return b;
      }
}

Output :

True

64. Add elements of digits:9999
Output : 9+9+9+9=3+6=9;

package com.jaladhi;
public class OneHundredEighteen {
      public static void main(String[] args) {
            int n = 9999;
            System.out.println(conversiontoaSingleDigit(n));
      }
      public static int conversiontoaSingleDigit(int n){
            loop:
            while(n > 10){
                  int l = 0,m = 0;
                  while(n != 0){
                        m = n%10;
                        l = l+m;
                        n = n/10;
                  }
                  n = l;
                  continue loop;
            }
            return n;
      }
}

Output :

9

65. Leap Year or not using API?

package com.jaladhi;
import java.util.*;
public class OneHundredNinteen {
      public static void main(String[] args) {
            String s="2013";
            System.out.println(leapYear(s));
      }
      public static boolean leapYear(String s) {
            int n=Integer.parseInt(s);
            GregorianCalendar c=new GregorianCalendar();
            boolean b=c.isLeapYear(n);
            return b;
      }
}

Output :

False

66. Perfect Number or not ?

package com.jaladhi;
public class OneHundredTwenty {
      public static void main(String[] args) {
            int n = 28;
            System.out.println(perfectNumber(n));
      }
      public static boolean perfectNumber(int n) {
            int n1 = 0;
            boolean b = false;
            for(int i = 1;i < n;i++)
                  if(n%i == 0)
                        n1 += i;
                        //System.out.println(n1);
            if(n1 == n)
                  b = true;
            return b;
      }
}

Output :

True

67. HashMap < String,String > input1 = {"mouse":"100.2","speaker":"500.6","Monitor":"2000.23"};
String[] input2 = {"speaker","mouse"};
Float output = 600.80(500.6+100.2);

package com.jaladhi;
import java.util.*;
public class OneHundredTwentyOne {
      public static void main(String[] args) {
            HashMap m1 = new HashMap();
            m1.put("mouse", "100.2");
            m1.put("speaker","500.6");
            m1.put("monitor", "2000.23");
            String[] s={"speaker","mouse"};
            System.out.println(getTheTotalCostOfPheripherals(m1,s));
      }
      public static float getTheTotalCostOfPheripherals(HashMap m1, String[] s) {
            Float f = (float) 0;
            Iterator i = m1.keySet().iterator();
            while(i.hasNext()){
                  String s1 = (String) i.next();
                  Float f1 = Float.parseFloat(m1.get(s1));
                  for(int j = 0;j < s.length;j++)
                        if(s[j].equals(s1))
                              f += f1;
            }
            return f;
      }
}

Output :

600.80

68.
Input 1 = 845.69
Output = 3:2

Input 1 = 20.789
Output = 2:3

Input1 = 20.0
Output = 2:1

Output is in Hashmap format.
Hint: count the no of digits.

package com.jaladhi;
import java.util.*;
public class OneHundredTwentyTwo {
      public static void main(String[] args) {
            double d = 845.69;
            System.out.println(noOfDigits(d));
      }
      public static String noOfDigits(double d) {
            int n1 = 0,n2 = 0;
            String s = String.valueOf(d);
            StringTokenizer t = new StringTokenizer(s,".");
            String s1 = t.nextToken();
            String s2 = t.nextToken();
            n1 = s1.length();
            n2 = s2.length();
            if(s1.charAt(0) == '0')
                  n1 = s1.length()-1;
            if(n2 != 1)
                  if(s2.charAt(s2.length()-1) == '0')
                        n2 = s2.length()-1;
            String s3 = String.valueOf(n1)+":"+String.valueOf(n2);
            return s3;
      }
}

Output :

3: 2

69. Nested for Loops Arch with Characters

package com.jaladhi;
import java.util.Scanner;
public class OneHundredTwentyThree {
      public static void main(String[] args) {
            int p, q, r, s;
            String ch;
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter any String : ");
            ch = sc.next();
            r = ch.length();
            for(p = 0; p < r; p++) {
                  for(q = 0; q < (r-p); q++)
                        System.out.print(" " + ch.charAt(q));
                  for(q = 0; q < (p*2); q++)
                        System.out.print(" ");
                  s = r - p -1;
                  for(q = s; q >= 0 ; q--)
                        System.out.print(" " + ch.charAt(q));
                  System.out.println();
            }
            sc.close();
      }
}

Output :

A B C D E F G H H G F E D C B A
A B C D E F G         G F E D C B A
A B C D E F         F E D C B A
A B C D E         E D C B A
A B C D         D C B A
A B C         C B A
A B         B A
A         A

70. For the given string display the middle 2 value, case satisfies only if string is of even length.

Input : java
Output : av
package com.jaladhi;
public class Seventy {
      public static void main(String[] args) {
            String s1 = "sumithra";
            System.out.println(printingSubstringOfMiddleChars(s1));
      }
      public static String printingSubstringOfMiddleChars(String s1) {
            String s2 = s1.substring((s1.length()/2)-1, (s1.length()/2)+1);
            return s2;
      }
}

Output :

it

71. Given an array int a[]. Add the sum at even indexes.do the same with odd indexes. If both the sum is equal return 1 or return -1.

package com.jaladhi;
public class SeventyOne {
      public static void main(String[] args) {
            int a[] = {9,8,5,3,2,6,4,7,5,1};
            System.out.println(oddEvenIndicesCount(a));
      }
      public static int oddEvenIndicesCount(int[] a) {
            int n1 = 0,n2 = 0,n3 = 0;
            for(int i = 0;i < a.length;i++)
                  if(i%2 == 0)
                        n1 += a[i];
                  else
                        n2 += a[i];
                  if(n1 == n2)
                        n3 = 1;
                  else
                        n3 = -1;
            return n3;
      }
}

Output :

1

72. Number of days in a month in specific year.

package com.jaladhi;
import java.util.*;
public class SeventyTwo {
      public static void main(String[] args){
            Calendar ca = new GregorianCalendar(2013,Calendar.FEBRUARY,03);
            System.out.println(noOfDaysInaMonth(ca));
      }
      public static int noOfDaysInaMonth(Calendar ca){
            int n = ca.getActualMaximum(Calendar.DAY_OF_MONTH);
      return n;
      }
}

Output :

28

73. Find the sum of the numbers in the given input string array.
Input : {"2AA","12","ABC","c1a")
Output : 6(2+1+2+1)
Note in the above array 12 must not considered as such that is it must be considered as 1,2.

package com.jaladhi;
public class SeventyThree {
      public static void main(String[] args) {
            String[] s1={"2AA","12","A2C","C5a"};
            getSum(s1);
      }
      public static void getSum(String[] s1) {
            int sum = 0;
            for(int i = 0;i < s1.length;i++)
                  for(int j = 0;j < s1[i].length(); j++){
                        char c = s1[i].charAt(j);
                        if(Character.isDigit(c)){
                              String t = String.valueOf(c);
                              int n = Integer.parseInt(t);
                              sum = sum + n;
                        }
                  }
            System.out.println(sum);
      }
}

Output :

12

74. Create a program to get the hashmap from the given input string array where the key for the hashmap is first three letters of array element in uppercase and the value of hashmap is the element itself.

Input : {"goa","kerala","gujarat"} [String Array]
Output : {{GOA,goa},{KER,kerala},{GUJ,Gujarat}} [HashMap]
package com.jaladhi;
import java.util.*;
public class SeventyFour {
      public static void main(String[] args) {
            String[] s1 = {"goa","kerala","gujarat"};
            putvalues(s1);
      }
      public static void putvalues(String[] s1) {
            ArrayList < String > l1 = new ArrayList < String > ();
            HashMap < String,String > m1 = new HashMap < String,String > ();
            ArrayList < String > l2 = new ArrayList < String > ();
            for(String s:s1)
                  l1.add(s.toUpperCase().substring(0, 3));
            for(String s:s1)
                  l2.add(s);
            for(int i = 0;i < l1.size();i++)
                  m1.put(l1.get(i),l2.get(i));
            System.out.println(m1);
      }
}

Output :

{GOA = goa, GUJ = gujarat, KER = kerala}

75. String[] input = ["Vikas","Lokesh",Ashok].
Expected Output String: "Vikas,Lokesh,Ashok"

package com.jaladhi;
public class SeventyFive {
      public static void main(String[] args) {
            String[] ip = {"Vikas","Lokesh","Ashok"};
            System.out.println(getTheNamesinGivenFormat(ip));
      }
      public static String getTheNamesinGivenFormat(String[] ip) {
            StringBuffer sb = new StringBuffer();
            for(int i = 0;i < ip.length;i++)
                  sb.append(ip[i]).append(',');
            sb.deleteCharAt(sb.length()-1);
            return sb.toString();
      }
}

Output :

Vikas, Lokesh, Ashok

76. Email Validation
String input1="test@gmail.com"
1)@ & . should be present;
2)@ & . should not be repeated;
3)there should be five charcters between @ and .;
4)there should be atleast 3 characters before @ ;
5)the end of mail id should be .com;

package com.jaladhi;
import java.util.*;
public class SeventySix {
      public static void main(String[] args) {
            String ip="test@gmail.com";
            boolean b=emailIdValidation(ip);
            if(b == true)
                  System.out.println("valid mail Id");
            else
                  System.out.println("not a valid Id");
      }
      public static boolean emailIdValidation(String ip) {
            int i=0;
            boolean b=false;
            StringTokenizer t=new StringTokenizer(ip,"@");
            String s1=t.nextToken();
            String s2=t.nextToken();
            StringTokenizer t1=new StringTokenizer(s2,".");
            String s3=t1.nextToken();
            String s4=t1.nextToken();
            if(ip.contains("@") && ip.contains("."))
                  i++;
            if(i==1)
                  if(s3.length()==5)
                        if(s1.length()>=3)
                              if(s4.equals("com"))
                                    b=true;
            return b;
      }
}

Output :

valid mail Id

77. Square root calculation of ((x1+x2)*(x1+x2))+((y1+y2)*(y1+y2)) output should be rounded of to int;

package com.jaladhi;
public class SeventySeven {
      public static void main(String[] args) {
            int x1 = 4,x2 = 8;
            int y1 = 3,y2 = 5;
            sqrt(x1,x2,y1,y2);
      }
      public static void sqrt(int x1, int x2, int y1, int y2) {
            int op;
            op = (int)(Math.sqrt((x1+x2)*(x1+x2))+((y1+y2)*(y1+y2)));
            System.out.println(op);
      }
}

Output :

76

78. Input hashmap < String String > = {"ram:hari","cisco:barfi","honeywell:cs","cts:hari"};
Input 2 = "hari";
Output : String[] = {"ram","cts"};

package com.jaladhi;
import java.util.*;
import java.util.Map.Entry;
public class SeventyEight {
      public static void main(String[] args) {
            HashMap < String,String > m1 = new HashMap < String,String > ();
            m1.put("ram","hari");
            m1.put("cisco","barfi");
            m1.put("honeywell","cs");
            m1.put("cts","hari");
            String s2 = "hari";
            getvalues(m1,s2);
      }
      public static void getvalues(HashMap < String, String > m1, String s2) {
            ArrayList < String > l1 = new ArrayList < String > ();
            for(Entry < String, String > m:m1.entrySet()){
                  m.getKey();
                  m.getValue();
                  if(m.getValue().equals(s2))
                        l1.add(m.getKey());
            }
            String[] op = new String[l1.size()];
            for(int i = 0;i < l1.size();i++){
                  op[i] = l1.get(i);
                  System.out.println(op[i]);
            }
      }
}

Output :

ram cts

79. Input 1 = {"ABX","ac","acd"};
Input 2 = 3;
Output 1 = X$d

package com.jaladhi;
import java.util.*;
public class SeventyNine {
      public static void main(String[] args) {
            String[] s1 = {"abc","da","ram","cat"};
            int ip = 3;
            getStr(s1,ip);
      }
      public static void getStr(String[] s1, int ip) {
            String op = " ";
            String s2 = " ";
            ArrayList < String > l1 = new ArrayList < String > ();
            for(String s:s1)
                  if(s.length() == ip)
                        l1.add(s);
            StringBuffer buff = new StringBuffer();
            for(String l:l1) {
                  s2 = l.substring(l.length()-1);
                  buff.append(s2).append("$");
            }
            op = buff.deleteCharAt(buff.length()-1).toString();
            System.out.println(op);
      }
}

Output :

c$m$t

80. Input 1 = helloworld
Input 2 = 2. delete the char,if rpted twice. If occurs more than twice,leave the first occurence and delete the duplicate.
Output = helwrd;

package com.jaladhi;
public class Eighty {
      public static void main(String[] args) {
            String input1 = "HelloWorld";
            int input2 = 2;
            System.out.println(deletingtheCharOccuringTwice(input1,input2));
      }
      public static String deletingtheCharOccuringTwice(String input1, int input2) {
            StringBuffer sb = new StringBuffer(input1);
            int c = 1;
            for(int i = 0;i < sb.length();i++){
                  c = 1;
                  for(int j = i+1;j < sb.length();j++)
                        if(sb.charAt(i) == sb.charAt(j))
                              c++;
                        if(c >= input2){
                              for(int j = i+1;j < sb.length();j++)
                                    if(sb.charAt(i) == sb.charAt(j))
                                          sb.deleteCharAt(j);
                              sb.deleteCharAt(i);
                              i--;
                        }
            }
            return sb.toString();
      }
}

Output :

HeWrd

81. String[] input = {"100","111","10100","10","1111"} input2="10".
Output = 2;count strings having prefix"10" but "10" not included in count.
Operation -- for how many strings input2 matches the prefix of each string in input1.
String[] input = {"01","01010","1000","10","011"}.
Output = 3; count the strings having prefix"10","01" but "10","01" not included.

package com.jaladhi;
import java.util.*;
public class EightyOne {
      public static void main(String[] args) {
            String[] ip = {"100","111","10100","10","1111"};
            gteCount(ip);
      }
      public static void gteCount(String[] ip) {
            int op = 0;
            ArrayList < String > l1 = new ArrayList < String > ();
            for(String s:ip)
                  if(s.startsWith("10") || s.startsWith("01") && (s.length()>2))
                        l1.add(s);
            op = l1.size();
            System.out.println(op);
      }
}

Output :

3

82. Input 1 = 1
Input 2 = 2
Input 3 = 3
Output = 6
Input 1 = 1
Input 2 = 13
Input 3 = 3
Output = 1
Input 1 = 13
Input 2 = 2
Input 3 = 8
Output = 8
If value equal to 13,escape the value '13', as well as the next value to 13. Sum the remaining values.

package com.jaladhi;
import java.util.*;
public class EightyTwo {
      public static void main(String[] args) {
            int ip1 = 13,ip2 = 2,ip3 = 8;
            System.out.println(thirteenLapse(ip1,ip2,ip3));
      }
      public static int thirteenLapse(int ip1, int ip2, int ip3) {
            List < Integer > l = new ArrayList < Integer > ();
            l.add(ip1);
            l.add(ip2);
            l.add(ip3);
            int s = 0;
            for(int i = 0;i < l.size();i++){
                  if(l.get(i) != 13)
                        s += l.get(i);
                  if(l.get(i) == 13)
                        i = i + 1;
            }
            return s;
      }
}

Output :

8

83. Input = "hello"
Output = "hlo"; Alternative positions...

package com.jaladhi;
public class EightyThree {
      public static void main(String[] args) {
            String s = "Hello";
            System.out.println(alternatingChar(s));
      }
      public static String alternatingChar(String s){
            StringBuffer sb = new StringBuffer();
            for(int i = 0;i < s.length();i++)
                  if(i%2 == 0)
                        sb.append(s.charAt(i));
            return sb.toString();
      }
}

Output :

Hlo

84. Input = "Hello World";
Output = à "dello WorlH".

package com.jaladhi;
public class EightyFour {
      public static void main(String[] args) {
            String s="Hello World";
            System.out.println(reArrangingWord(s));
      }
      public static String reArrangingWord(String s) {
            StringBuffer sb=new StringBuffer();
            sb.append(s.substring(s.length()-1));
            sb.append(s.substring(1, s.length()-1));
            sb.append(s.substring(0, 1));
            return sb.toString();
      }
}

Output :

dello WorlH

85. Collect no & #39s from list 1 which is not present in list 2 and Collect no & #39s from list 2 which is not present in list 1 and store it in output 1[].

Example : Input 1 = {1,2,3,4}; Input 2 = {1,2,3,5}; Output 1 = {4,5};
package com.jaladhi;
import java.util.*;
public class EightyFive {
      public static void main(String[] args) {
            List < Integer > l1 = new ArrayList < Integer > ();
            l1.add(1);
            l1.add(2);
            l1.add(3);
            l1.add(4);
            List < Integer > l2 = new ArrayList < Integer > ();
            l2.add(1);
            l2.add(2);
            l2.add(3);
            l2.add(5);
            int o[] = commonSet(l1,l2);
            for(int i:o)
                  System.out.println(i);
      }
      public static int[] commonSet(List < Integer > l1, List < Integer > l2) {
            List < Integer > l3 = new ArrayList < Integer > ();
            List < Integer > l4 = new ArrayList < Integer > ();
            l3.addAll(l1);
            l4.addAll(l2);
            l1.removeAll(l2);
            l4.removeAll(l3);
            l1.addAll(l4);
            int o[]=new int[l1.size()];
            for(int j = 0;j < o.length;j++)
                  o[j] = l1.get(j);
            return o;
      }
}

Output :

4 5

86. String array will be given.if a string is Prefix of an any other string in that array means count.

package com.jaladhi;
public class EightySix {
      public static void main(String[] args) {
            String[] a = {"pinky","preethi","puppy","preeth","puppypreethi"};
            System.out.println(namesWithPreFixes(a));
      }
      public static int namesWithPreFixes(String[] a) {
            int n = 0;
            for(int i = 0;i < a.length;i++)
                  for(int j = i+1;j < a.length;j++){
                        String s1 = a[i];
                        String s2 = a[j];
                        if(s2.startsWith(s1)||s1.startsWith(s2))
                              n++;
                  }
            return n;
      }
}

Output :

2

87. Count the number of words in the string.
Input String = "i work in cognizant.";
Output = 4;

package com.jaladhi;
import java.util.StringTokenizer;
public class EightySeven {
      public static void main(String[] args) {
            String s = "I work for cognizant";
            System.out.println(noOfWordsInString(s));
      }
      public static int noOfWordsInString(String s) {
            StringTokenizer t = new StringTokenizer(s," ");
            return t.countTokens();
      }
}

Output :

4

88. int[] input = {2,1,4,1,2,3,6};
Check whether the input has the sequence of "1,2,3". if so
Output = true;
int[] input = {1,2,1,3,4,5,8};
Output = false

package com.jaladhi;
public class EightyEight {
      public static void main(String[] args) {
            //int[] a = {2,1,4,1,2,3,6};
            int[] a = {1,2,1,3,4,5,8};
            System.out.println(sequenceInArray(a));
      }
      public static boolean sequenceInArray(int[] a) {
            boolean b = false;
            int n = 0;
            for(int i = 0;i < a.length-1;i++)
                  if((a[i+1]-a[i]) == 1)
                        n++;
                  if(n == 2)
                        b = true;
            return b;
      }
}

Output :

false

89. Input :
String input1 = "AAA/abb/CCC"
char input2 = '/'
Output :
String[] output1;
Output1[] = {"aaa","bba","ccc"};
Operation : get the strings from input1 using stringtokenizer reverse each string then to lower case finally store it in output1[] string array.

package com.jaladhi;
import java.util.*;
public class ClassSeT17 {
      public static void main(String[] args) {
            String ip1 = "AAA/abb/CCC";
            char ip2 = '/';
            String op[] = loweringCasenReverseofaString(ip1,ip2);
            for(String s:op)
                  System.out.println(s);
      }
      public static String[] loweringCasenReverseofaString(String ip1, char ip2){
            List < String > l = new ArrayList < String > ();
            StringTokenizer t = new StringTokenizer(ip1,"/");
            while(t.hasMoreTokens()){
                  StringBuffer sb = new StringBuffer(t.nextToken().toLowerCase());
                  l.add(sb.reverse().toString());
            }
            String op[] = new String[l.size()];
            for(int i = 0;i < op.length;i++)
                  op[i] = l.get(i);
                  return op;
      }
}

Output :

aaa
bba
ccc

90. Input 1 = "cowboy";
Output 1 = "cowcow";
Input 1 = "so";
Output 1 = "sososo";
HINT: if they give 3 letter word u have to display 2 time;

package com.jaladhi;
public class Ninty {
      public static void main(String[] args) {
            String ip1 = "cowboy";
            String ip2 = "cow";
            System.out.println(printingStringDependingOncharCount(ip1,ip2));
      }
      public static String printingStringDependingOncharCount(String ip1,String ip2) {
            StringBuffer sb = new StringBuffer();
            int n1 = ip2.length();
            if(n1 == 3)
                  for(int i = 0;i < n1-1;i++)
                        sb.append(ip1.substring(0, n1));
            else if(n1 == 2)
                  for(int i = 0;i < n1+1;i++)
                        sb.append(ip1.substring(0, n1));
            return sb.toString();
      }
}

Output :

cowcow