207 Jva Programs
207 Jva Programs
207 Jva Programs
int[] a={10,15,20,25,30,100};
int b=15;
int i,sum=0,r=0;
for(i=0;i<a.length;i++)
{
if(a[i]>b)
{
sum=sum+a[i];
}
}
//System.out.println(sum);
while(sum!=0)
{
r=(r*10)+(sum%10);
sum=sum/10;
}
System.out.println(r);;
}
2.Add Time
********************************************************
public class Main {
String s1="23:59:59";
String s2="23:59:59";
SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
sdf.setTimeZone(TimeZone.getTimeZone("s1"));
sdf.setTimeZone(TimeZone.getTimeZone("s2"));
Date d1=sdf.parse(s1);
Date d2=sdf.parse(s2);
long add=d1.getTime()+d2.getTime();
String s=sdf.format(add);
Calendar cal=Calendar.getInstance();
cal.setTime(sdf.parse(s));
int FindDay=cal.get(Calendar.DAY_OF_MONTH);
if(FindDay>1)
FindDay=FindDay-1;
String op=FindDay+":"+s;
System.out.println(op);
}
}
3.Adjacent Swaps
*********************************************************
public class Main
{
public static void main(String[] args) {
String s1="forget";
getvalues(s1);
}
public static void getvalues(String s1)
{
StringBuffer sb=new StringBuffer();
int l=s1.length();
if(l%2==0)
{
for(int i=0;i<s1.length()-1;i=i+2)
{
char a=s1.charAt(i);
char b=s1.charAt(i+1);
sb.append(b).append(a);
}
System.out.println(sb);
}
else
{
for(int i = 0;i<s1.length()-1;i=i+2)
{
char a=s1.charAt(i);
char b=s1.charAt(i+1);
sb.append(b).append(a);
System.out.println(sb);
}
sb.append(s1.charAt(l-1));
System.out.println(sb);
}
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
public class UserMainCode {
public static String display(String n)
{
int year=0;
String now="01/01/2015";
SimpleDateFormat sdf1=new SimpleDateFormat("dd/MM/yyyy");
try
{
Date d=sdf1.parse(n);
Date d1=sdf1.parse(now);
sdf1.setLenient(false);
int y=d.getYear();
int y1=d1.getYear();
int m=d.getMonth();
int m1=d1.getMonth();
int day=d.getDay();
int day1=d1.getDay();
year=y1-y;
if(m>m1)
year--;
else if(m==m1)
{if(day<day1)
year--;
}
}
catch(Exception e)
{
e.printStackTrace();
}
if(year>18)
return "eligible";
else
return "not-eligible";
}}
5.All Numbers(validateNumber)
**************************************************
import java.util.Scanner;
}
if(k==s.length())
c++;
}
if(c==a.length)
return 1;
else
return -1;
}}
6.All Vowels
**********************************************************
import java.util.Iterator;
import java.util.LinkedHashSet;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
String s1="alphabet";
getvalues(s1);
}
public static void getvalues(String s1) {
String s2="aeiou";
StringBuffer sb=new StringBuffer();
for(int i=0;i<s1.length();i++)
{
for(int j=0;j<s2.length();j++)
{
if(s1.charAt(i)==s2.charAt(j))
{
sb.append(s1.charAt(i));
}
}
}
if(sb.toString().equals(s2))
{
System.out.println("true");
}
else
System.out.println("false");
}
}
7.8.Anagrams
********************************************************
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
}
}
import java.util.ArrayList;
import java.util.Collections;
public class kape1 {
public static ArrayList<Integer> answer (ArrayList<Integer> al1, ArrayList<Integer>
al2)
{
al1.addAll(al2);
Collections.sort(al1);
ArrayList<Integer> ans=new ArrayList<Integer>();
ans.add(al1.get(2));
ans.add(al1.get(6));
ans.add(al1.get(8));
return ans;
}
10.ArrayFront (scanArray)
***************************************************************************
6/1/2/3/4/5/6
import java.util.Scanner;
return b;
}
}
import java.util.ArrayList;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Iterator;
public class Usermaincode
{
public static ArrayList<Integer> display
(ArrayList<Integer>al1,ArrayList<Integer>al2)
{
ArrayList<Integer>al3=new ArrayList<Integer>();
for(int i=0;i<al1.size();i++)
{
if(i%2==0)
al3.add(al2.get(i));
else
al3.add(al1.get(i));
}
return al3;
}}
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Iterator;
public class kapes4
{public static String display(HashMap<Integer,Float>hm)
{
float sum=0;
int count=0;
DecimalFormat df=new DecimalFormat("#.00");
Iterator<Integer> it=hm.keySet().iterator();
while(it.hasNext())
{
int y=it.next();
if(y%2==0)
{
sum=(float) (sum+hm.get(y));
count++;
}}
float d=sum/count;
return df.format(d);
}
}
System.out.print(kape1.display(n,a));
}}
for(int i=2;i<n;i++)
{
count=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
count++;
}
}
if(count==2)
{
sum=sum+a[i];
n1=n1+1;
}
}
avg=(double)(sum)/n1;
return avg;
}}
import java.util.Scanner;
System.out.println(kapes4.getvalues(arr, n));
}}
public class kapes4{
public static int getvalues(int[] a,int n) {
int sum=0;
int count=0;
int sum_count=0;
for(int i=0;i<a.length;i++)
{
count=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
count++;
if(i<0)
break;
}
}
if(count==2)
{
sum=sum+a[i];
sum_count++;
}
}
int avg=sum/sum_count;
return avg;
}}
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
class main13
{
public static double dis(HashMap<Integer,Double> h1)
{
double avg=0.0,sum=0.0;
int k=0;
for(Map.Entry m:h1.entrySet())
{
int a=(Integer)m.getKey();
if(a%2!=0)
{
Double d=(Double) m.getValue();
sum=sum+d;
k++;
}
}
avg = (double)sum/k;
DecimalFormat df = new DecimalFormat(".##");
String b1 = df.format(avg);
double b = Double.parseDouble(b1);
return b;}}
System.out.println(Usermaincode.display(age));
}
}
import java.util.StringTokenizer;
public class Usermaincode
{public static int display(String s)
{
int count=0;
String fin="";
StringTokenizer st=new StringTokenizer(s);
String ini=st.nextToken();
while(st.hasMoreTokens())
{ fin=st.nextToken();
}
if(ini.equals(fin))
count=ini.length();
else
count=ini.length()+fin.length();
return count;
}
}
return n3;
}
public static void main(String[] args) {
int n=84882;
System.out.println(SumOfOddsAndEvens(n));
}
}
27.28.Color Code ()
*******************************************
import java.util.*;
public class B {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
String s1=s.next();
boolean b=colorCodeValidation(s1);
if(b==true)
System.out.println("valid color code");
else
System.out.println("invalid color code");
}
public static boolean colorCodeValidation(String s1) {
boolean b=false,b1=false;
String s2=s1.substring(1,s1.length());
if(s1.length()==7)
if(s1.charAt(0)=='#')
b1=true;
if(b1==true)
for(int i=0;i<s2.length();i++){
char c=s2.charAt(i);
if(c!='#'){
if((Character.isAlphabetic(c)&& Character.isUpperCase(c)) || Character.isDigit(c))
b=true;
else{
b=false;
break;}}}
return b;
}
}}
int sum=0;
for(int i=0;i<a.length;i++)
{
for(int j=0;j<b.length;j++)
{
if(a[i]==b[j])
sum=sum+a[i];
}}
if(sum==0)
return -1;
else
return sum;
}}
30.Commons (countCommonStrings)
***************************
public class kape {
32.Convert Format
****************************************************
import java.util.StringTokenizer;
public class Main {
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();
return sb.toString();
}
}
35.Dash Check
**********************************************
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
String s1="hi-there-you.";
String s2="12-(134)-7539";
getvalues(s1,s2);
}
public static void getvalues(String s1, String s2) {
ArrayList<Integer>l1=new ArrayList<Integer>
();
for(int i=0;i<s1.length();i++)
{
if(s1.charAt(i)=='-')
{
l1.add(i);
}
}
ArrayList<Integer>l2=new ArrayList<Integer>
();
for(int i=0;i<s2.length();i++)
{
if(s2.charAt(i)=='-')
{
l2.add(i);
}
}
//System.out.println(l1);
//System.out.println(l2);
if(l1.equals(l2))
{
System.out.println(1);
}
else
System.out.println(-1);
}
}
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
// TODO Auto-generated method stub
String s1="12/07/1994";
String s2="12/07/1995";
SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat sdf1=new SimpleDateFormat("MM-dd-yyyy");
Date d1=sdf.parse(s1);
Date d2=sdf.parse(s2);
Calendar cal=Calendar.getInstance();
cal.setTime(d1);
long y=cal.getTimeInMillis();
cal.setTime(d2);
long y1=cal.getTimeInMillis();
String s3=sdf1.format(d1);
String s4=sdf1.format(d2);
if(y<y1)
System.out.println(s3);
else
System.out.println(s4);
}
}
40.Day of Week
************************************************************
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
System.out.println(display(array));
}
public static int display(int []array)
{
Arrays.sort(array);
int n=array[array.length-1]-array[0];
int b=array.length;
if(b==1)
{
n=array[0];
}
return n;
}
}
43.Difference between two dates in days (getDateDifference)
***********************************************************************************
2012-03-12/2012-03-14/2
import java.text.*;
import java.util.*;
public class Main {
public static int dateDifference(String s1,String s2) throws ParseException{
SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd");
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="2012-03-12";
String s2="2012-03-14";
System.out.println(dateDifference(s1,s2));
}
}
int a=89;
int b=59;
System.out.println(characterCheck(a,b));
}
public static boolean characterCheck(int q,int w) {
boolean b=false;
int c=q%10;
int d=w%10;
if(c==d)
b=true;
else
b=false;
return b;
}
}
45.Digits - II
**********************************************************
public class Main
{
public static void main(String[] args)
{
int n=698;
System.out.println(conversiontoaSingleDigit(n));
}
public static int conversiontoaSingleDigit(int n)
{
int sum = 0 ;
int n1=n;
while(n>10)
{
int a = 0 ; sum = 0;
while(n!=0)
{
a = n%10;
sum+=a;
n=n/10;
}
n=sum;
}
return sum;
}
}
46.Digits (countSeven)
***********************************************************************************
717/2
import java.util.Scanner;
import java.util.TreeMap;
import java.util.Scanner;
}
TreeMap<String,Integer>tm=new TreeMap<String,Integer>();
tm=Usermaincode.display(hm,hm1);
Iterator<String> it=tm.keySet().iterator();
while(it.hasNext())
{
String n=it.next();
int fac=tm.get(n);
System.out.println(n+":"+fac);
}
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.HashMap;
import java.util.TreeMap;
public class Usermaincode
{
public static TreeMap<String,Integer> display
(HashMap<String,String>hm,HashMap<String,Integer>hm1)
{
int year=0,amount=0;
double dis=0;
String now="01/01/2015";
TreeMap<String,Integer>tm=new TreeMap<String,Integer>();
Iterator<String> it=hm.keySet().iterator();
while(it.hasNext())
{
String id=it.next();
String dor=hm.get(id);
amount=hm1.get(id);
SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat sdf1=new SimpleDateFormat("dd/MM/yyyy");
try
{
Date d=sdf.parse(dor);
Date d1=sdf1.parse(now);
sdf.setLenient(false);
int y=d.getYear();
int y1=d1.getYear();
int m=d.getMonth();
int m1=d1.getMonth();
int day=d.getDay();
int day1=d1.getDay();
year=y1-y;
if(m>m1)
year--;
else if(m==m1)
{if(day<day1)
year--;
}
if(year>=5 && amount>=20000)
dis=0.2*amount;
else if(year<5 && amount>=20000)
dis=0.1*amount;
else if(year>=5 && amount<20000)
dis=0.15*amount;
else
dis=0.05*amount;
tm.put(id,(int)dis);
}
catch(Exception e)
{
e.printStackTrace();
}
}
return tm;
}
}
48.DOB - Validation(ValidateDOB)
*****************************
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class UserMainCode {
public static void main(String[] args)
{
String str=new String();
Scanner sc=new Scanner(System.in);
str=sc.nextLine();
SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(false);
try
{
Date d1=sdf.parse(str);
System.out.println("TRUE");
}
catch(Exception e)
{
System.out.println("FALSE");
}
}
}
49.Duplicate Characters
*********************************************
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
String s1="hi this is sample test";
getvalues(s1);
}
public static void getvalues(String s1) {
char a[]=s1.toCharArray();
StringBuffer sb=new StringBuffer();
LinkedHashSet<Character>hs=new LinkedHashSet<Character>();
for(int i=0;i<a.length;i++)
{
hs.add(a[i]);
}
Iterator<Character>itr=hs.iterator();
while(itr.hasNext())
{
char o=itr.next();
if(o!=' ');
{
sb.append(o);
}
}
System.out.println(sb);
}}
50.Duplicates (getDistinctSum)
*********************************************************************************
1/2/1/2
import java.util.*;
public class User {
public static void main(String[] args) {
int a=1;
int b=1;
int c=1;
System.out.println(characterCheck(a,b,c));
}
public static int characterCheck(int q,int w,int e) {
int s=0;
if(q!=w&&q!=e)
{
s=q+w+e;
}
else if(q==w&&q==e)
{
s=0;
}
else if(q==w&&q!=e)
{
s=e;
}
else if(q!=w&&q==e)
{
s=w;
}
return s;
}
}
51.Elements in ArrayList(arrayListSubtractor)
*********************************************
import java.util.*;
int count=0,key;
int max = arrlist1.size();
if(arrlist1.size() < arrlist2.size())
max = arrlist2.size();
ArrayList<Integer> temp = new ArrayList<Integer>(max);
for(int i=0;i<arrlist1.size();i++)
{
key = (int)arrlist1.get(i);
if(arrlist2.indexOf(key) == -1)
{
++count;
temp.add(key);
}
}
for(int i=0;i<arrlist2.size();i++)
{
key = (int)arrlist2.get(i);
if(arrlist1.indexOf(key) == -1)
{
if(!temp.contains(key))
{
++count;
temp.add(key);
}
}
}
int[] result = new int[count];
for(int i=0;i<count;i++)
result[i] = (int)temp.get(i);
return result;
}
}
import java.util.StringTokenizer;
if(i==1)
if(s3.length()==5)
if(s1.length()>=3)
if(s4.equals("com"))
b=true;
return b;
}
}
53.Employee Bonus
*********************************************************************
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeMap;
public class Main
{
public static void main(String args[])
{
HashMap<Integer,String>hm=new HashMap<Integer,String>();
HashMap<Integer,Integer>hm1=new HashMap<Integer,Integer>();
TreeMap<Integer,Integer>hm2=new TreeMap<Integer,Integer>();
Scanner sc=new Scanner(System.in);
int s=Integer.parseInt(sc.nextLine());
for(int i=0;i<s;i++)
{
int id=Integer.parseInt(sc.nextLine());
String ss=sc.nextLine();
int salary=Integer.parseInt(sc.nextLine());
hm.put(id,ss);
hm1.put(id,salary);
}
hm2=User.display(hm,hm1);
Iterator<Integer> it=hm2.keySet().iterator();
for(int i=0;i<s;i++)
{
int id=it.next();
int bonus=hm2.get(id);
System.out.println(id);
System.out.println(bonus);
}
}}
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.TreeMap;
public class User
{ public static TreeMap<Integer,Integer>
display(HashMap<Integer,String>hm,HashMap<Integer,Integer>hm1)
{
TreeMap<Integer,Integer>hm2=new TreeMap<Integer,Integer>();
Iterator<Integer> it=hm.keySet().iterator();
while(it.hasNext())
{
int y=it.next();
String dob=hm.get(y);
SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy");
sdf.setLenient(false);
String now="01-09-2014";
try
{ Date d=sdf.parse(now);
Date d1=sdf.parse(dob);
int y1=d.getYear();
int y2=d1.getYear();
int m1=d.getMonth();
int m2=d1.getMonth();
int day1=d.getDay();
int day2= d1.getDay();
int age=y1-y2;
if(m1<m2)
age--;
else if(m1==m2 && day1<day2)
age--;
if(age>=25 && age<=30)
{float bonus=(float)0.2*hm1.get(y)+hm1.get(y);
hm2.put(y,(int)bonus );
}
else if(age>30 && age<=60)
{ float bonus=(float) (0.3*hm1.get(y))+hm1.get(y);
hm2.put(y,(int)bonus );
}
else if(age<25 || age>60)
hm2.put(y,-200);
else
hm2.put(y,-100);
}
catch(Exception e)
{e.printStackTrace();
}}
return hm2;
}
while(it.hasNext())
{
String s2=it.next();
System.out.println(s2);
}
}
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
public class UserMainCode
{
public static LinkedHashMap<String,String> dis(LinkedHashMap<String,String>
h1,String n)
{
int k=0;
LinkedHashMap<String,String> hm1=new LinkedHashMap<String,String>();
Iterator<String>it=h1.keySet().iterator();
while(it.hasNext())
{
String s2=it.next();
String s3=h1.get(s2);
if(s3.equals(n))
hm1.put(s2,s3);
}
return hm1;
}}
import java.util.Iterator;
import java.util.LinkedHashSet;
n=sc.nextInt();
sc.nextLine();
System.out.println(display(s,s1,n));
}
58.Experience Validator(validateExp)
************************************
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
String s=sc.nextLine();
String s1=sc.nextLine();
System.out.print(UserMainCode.getvalues(s,s1));
}
}
import java.util.Calendar;
import java.util.Date;
int y=Math.abs(y1-y2);
int e=Integer.parseInt(s1);
if(y>=e)
return true;
else
return false;
}}
import java.util.Scanner;
}
return sb.toString();
}
}
60.Fibonacci Sum
**********************************************
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static int sumOfFibonacci(int n){
int a=0,b=1,c=0,d=1;
for(int i=3;i<=n;i++){
c=a+b;
a=b; b=c;
d=d+c;
}
return d;
}
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n=s.nextInt();
System.out.println(sumOfFibonacci(n));
}
}
}
}
63.Find Digits
****************************************************
import java.util.*;
public class Main {
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;
}
}
long q=(int)Math.round(Math.sqrt(((a-c)*(a-c))+((b-d)*(b-d))));
return (int) q;
}
}
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;
}
}
import java.util.ArrayList;
import java.util.Collections;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
String []a={"grape","mango","apple"};
String b="apple";
getvalues(a,b);
}
public static void getvalues(String[] a, String b) {
ArrayList<String>al=new ArrayList<String>();
for(int i=0;i<a.length;i++)
{
al.add(a[i]);
}
System.out.println(al);
Collections.sort(al);
System.out.println(al);
Collections.reverse(al);
System.out.println(al);
for(int i=0;i<al.size();i++)
{
if(b.equals(al.get(i)))
{
System.out.println(i+1);
}
}
}
}
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
public class UserMainCode {
public static String calculateBornDay(String s1) throws ParseException
{
SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat sdf1=new SimpleDateFormat("EEEEE");
Date d=sdf.parse(s1);
String s=sdf1.format(d);
return s.toUpperCase();
}
}
{
public static void main(String[]arg)
{
LinkedHashMap<String,Double>hm=new LinkedHashMap<String,Double>();
LinkedHashMap<String,String>hm1=new LinkedHashMap<String,String>();
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=0;i<n;i++)
{
String s=sc.next();
double d=sc.nextDouble();
hm.put(s,d);
}
LinkedHashMap<String,String>hm2=UserMainCode.dis(hm);
for(Map.Entry<String,String>entry:hm2.entrySet())
{
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}}}
import java.util.LinkedHashMap;
import java.util.Map;
class UserMainCode
{
public static LinkedHashMap<String,String>dis(LinkedHashMap<String,Double>h1)
{
LinkedHashMap<String,String>h2=new LinkedHashMap<String,String>();
for(Map.Entry m:h1.entrySet())
{
double d=(Double)m.getValue();
if(d>60)
{
String s=(String)m.getKey();
h2.put(s,"pass");
}
else
{
String s=(String)m.getKey();
h2.put(s,"fail");
}
}
return h2;
}
}
72.Grade Calculator
*****************
import java.util.HashMap;
import java.util.Iterator;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.Scanner;
import java.util.Iterator;
import java.util.HashMap;
import java.util.TreeMap;
public class Usermaincode
{
public static TreeMap<Integer,String> display (HashMap<Integer,Integer>hm)
{
TreeMap<Integer,String>tm=new TreeMap<Integer,String>();
Iterator<Integer> it=hm.keySet().iterator();
while(it.hasNext())
{
int id=it.next();
int mark=hm.get(id);
if(mark>=80)
tm.put(id,"GOLD");
else if(mark<80 && mark>=60)
tm.put(id,"SILVER");
else if(mark<60 && mark>=45)
tm.put(id,"BRONZE");
else
tm.put(id,"FAIL");
}
return tm;
}}
75.Integer Factorial(getFactorial)
**********************************
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Scanner;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
public class kapes4
{public static LinkedHashMap<Integer,Integer> display(int[] a)
{
LinkedHashMap<Integer,Integer>hm=new LinkedHashMap<Integer,Integer>();
for(int i=0;i<a.length;i++)
{
int u=1;
for(int j=1;j<=a[i];j++)
{u=u*j;}
hm.put(a[i],u);
}
return hm;
}}
76.Interest Calculation
*********************
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeMap;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.HashMap;
import java.util.TreeMap;
public class Usermaincode
{
public static TreeMap<String,Integer> display
(HashMap<String,String>hm,HashMap<String,Integer>hm1)
{
int year=0,amount=0;
double dis=0;
String now="01/01/2015";
TreeMap<String,Integer>tm=new TreeMap<String,Integer>();
Iterator<String> it=hm.keySet().iterator();
while(it.hasNext())
{
String id=it.next();
String dor=hm.get(id);
amount=hm1.get(id);
SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat sdf1=new SimpleDateFormat("dd/MM/yyyy");
try
{
Date d=sdf.parse(dor);
Date d1=sdf1.parse(now);
sdf.setLenient(false);
int y=d.getYear();
int y1=d1.getYear();
int m=d.getMonth();
int m1=d1.getMonth();
int day=d.getDay();
int day1=d1.getDay();
year=y1-y;
if(m>m1)
year--;
else if(m==m1)
{if(day<day1)
year--;
}
if(year>=60)
dis=0.1*amount+amount;
else if(year<60 && year>=30 )
dis=0.07*amount+amount;
else
dis=0.04*amount+amount;
tm.put(id,(int)dis);
}
catch(Exception e)
{
e.printStackTrace();
}
}
return tm;
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
String ipAddress="10.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;
}
}
78.ISBN Validation
***********************************
public class Main {
public static void main(String[] args) {
String ip="020110311";
boolean b=ISBNnumber(ip);
if(b==true)
System.out.println("valid ISBN number");
else
System.out.println("check ur data");
}
public static boolean ISBNnumber(String ip) {
boolean b=false;
int sum=0;
for(int i=0,j=ip.length();i<ip.length();i++,j--){
String s=String.valueOf(ip.charAt(i));
int n=Integer.parseInt(s);
sum+=(n*j); }
//System.out.println(sum);
if(sum%11==0)
b=true;
return b;
}
}
System.out.println(useerm.display(n));
}}
}
else
{
j=2;
}
return j;
}}
80.Largest Chunk
***************************************
import java.util.*;
public class Main {
public static void main(String[] args) {
String s1="this is soooo good";
System.out.println(maxChunk(s1));
}
public static int maxChunk(String s1) {
int max=0;
StringTokenizer t=new StringTokenizer(s1," ");
while(t.hasMoreTokens()){
String s2=t.nextToken();
int n=0;
for(int i=0;i<s2.length()-1;i++)
if(s2.charAt(i)==s2.charAt(i+1))
n++;
if(n>max)
max=n;
}
return (max+1);
}
}
}
public static int display(int []a)
{
int max=0;
int x,y,z;
x=a[0];
y=a[a.length/2];
z=a[a.length-1];
if(x>y&&x>z)
max=x;
else if(y>x&&y>z)
max=y;
else if(z>x&&z>y)
max=z;
return max;
}}
84.Largest Span(getLargestSpan)
****************************
import java.util.Scanner;
int n=sc.nextInt();
int []a=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.print(UserMainCode.display(a,n));
}}
public class UserMainCode {
public static int display(int[] x,int n)
{
int gap=0,max=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(x[i]==x[j])
{
gap=j;
}
}
if(gap-i>max)
max=gap-i;
}
return max+1;
}
86.Last letters(getLastLetter)
******************************
import java.util.Scanner;
String number=sc.nextLine();
System.out.print(Usermaincode.display(number));
}}
import java.util.StringTokenizer;
87.Leap Year
*********************************
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
String s1="23/02/2012";
SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
GregorianCalendar g=new GregorianCalendar();
StringTokenizer t=new StringTokenizer(s1,"/");
String s2=t.nextToken();
String s3=t.nextToken();
String s4=t.nextToken();
int n1=Integer.parseInt(s4);
Date d1=sdf.parse(s1);
boolean b=g.isLeapYear(n1);
System.out.println(b);
}
}
90.Longest Word
*****************************
import java.util.*;
public class Main {
public static void main(String[] args) {
String s1="ABC DEF";
System.out.println(lengthiestString(s1));
}
public static String lengthiestString(String s1) {
int max=0;
String s2=new String();
StringTokenizer t=new StringTokenizer(s1," ");
loop:
while(t.hasMoreTokens()){
String s3=t.nextToken();
int n=s3.length();
if(n>max){
max=n;
s2=s3;}
if(n==max)
for(int i=0;i<s3.length();i++){
char c1=s2.charAt(i);
char c2=s3.charAt(i);
if(c1!=c2){
if(c2<c1)
s2=s3;
continue loop;} }
}
return s2;
}}
import java.util.HashMap;
import java.util.Iterator;
import java.util.HashMap;
public class Usermaincode
{public static HashMap<Integer,Integer>
display(HashMap<Integer,String>hm,HashMap<Integer,Integer>hm1)
{
HashMap<Integer,Integer>hm3=new HashMap<Integer,Integer>();
Iterator<Integer> it=hm.keySet().iterator();
while(it.hasNext())
{
int id=it.next();
String name=hm.get(id);
if(name.equals("manager"))
{int salary=hm1.get(id)+5000;
hm3.put(id,salary);
}}
return hm3;
}
}
92.Mastering Hashmap
******************
import java.util.HashMap;
import java.util.Scanner;
public class kapes3
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
HashMap<Integer,Integer> h1=new HashMap<Integer,Integer>();
for(int i=0;i<n;i++)
{
h1.put(sc.nextInt(),sc.nextInt());
}
System.out.println(UserMainCode.display(h1));
}}
import java.util.HashMap;
import java.util.Iterator;
}
public static int display(int a,int b,String c)
{
int a1=0;
if(c.equals("*"))
{
a1=a*b;
}
else if(c.equals("+"))
{
a1=a+b;
}
else if(c.equals("-"))
{
a1=a-b;
}
else if(c.equals("/"))
{
a1=a/b;
}
else if(c.equals("%"))
{
a1=a%b;
}
return a1;
}}
94.Max Admissions
***********************************
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.ArrayList;
95.Max Scorer
**************************************
import java.util.StringTokenizer;
public class Main {
public static String retrieveMaxScoredStudent(String[] s1){
int max=0;
String s4=null;
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;
if(n>max)
{
max=n;
s4=s3;
}
}
return s4;
}
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));
}
}
96.Max Substring
**********************************
import java.util.*;
public class PO {
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
String s1=sc.nextLine();
int max=0;
String s3=null;
StringTokenizer st=new StringTokenizer(s,"-");
while( st.hasMoreTokens())
{
String s2=st.nextToken();
int n=s2.length();
if(n>max)
{
max=n;
s3=s2;
}
}System.out.print(s3);
}}
97.Max Vowels
***********************************************
import java.util.StringTokenizer;
98.Maximum Difference
****************************************
public class Main {
public static int getDiffArray(int[] n1){
int n2,n3=0,n4=0,i;
for(i=0;i<n1.length-1;i++){
n2=Math.abs(n1[i]-n1[i+1]);
if(n2>n3){
n3=n2;
n4=i+1; }}
return n4;
}
public static void main(String[] args) {
int[] n1={4,8,6,1,9,4};
System.out.println(getDiffArray(n1));
}
}
99.Median Calculation(calculateMedian)
***********************************
import java.util.*;
}
}
import java.util.Calendar;
public class main13{
public static int display(int year,int month)
{
Calendar cal=Calendar.getInstance();
cal.set(Calendar.YEAR,year);
cal.set(Calendar.MONTH,month);
int DAY_OF_MONTH=cal.getActualMaximum(cal.DAY_OF_MONTH);
return DAY_OF_MONTH;
}}
import java.text.ParseException;
import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
public class User {
public static String calculateBornDay(String s1) throws ParseException
{
SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yy");
SimpleDateFormat sdf1=new SimpleDateFormat("MMMM");
Date d=sdf.parse(s1);
String s=sdf1.format(d);
return s.toUpperCase();
}
}
104.nCr (calculateNcr)
********************************************************************** 4/3/4
public class Main {
public static void main(String[] args) {
int n=4;
int r=3;
getvalues(n,r);
}
public static void getvalues(int n, int r) {
int fact=1,fact1=1,fact2=1;
for(int i=1;i<=n;i++)
{
fact=fact*i;
}
//System.out.println(fact);
for(int i=1;i<=r;i++)
{
fact1=fact1*i;
}
//System.out.println(fact1);
for(int i=1;i<=(n-r);i++)
{
fact2=fact2*i;
}
//System.out.println(fact2);
int res=fact/(fact1*fact2);
System.out.println(res);
}
}
108.Occurance Count
**************************************
import java.io.IOException;
import java.text.ParseException;
import java.util.*;
110.Palindrome - In Range
*****************************************************
import java.util.*;
public class Main {
public static int sumOfPalindromeNos(int n1,int n2){
List<Integer> l1=new ArrayList<Integer>();
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));
}
}
import java.util.Iterator;
import java.util.LinkedHashSet;
public class useerm {
public static int display(String s) {
StringBuffer sb=new StringBuffer(s);
int k=0;
LinkedHashSet<Character>l1=new LinkedHashSet<Character>();
String s2=sb.reverse().toString();
if(s2.equals(s))
{
String s3=s2.toLowerCase();
for(int i=0;i<s3.length();i++)
{
l1.add(s3.charAt(i));
}
Iterator<Character> it=l1.iterator();
while(it.hasNext())
{
char a=it.next();
if(a=='a'||a=='e'||a=='i'||a=='o'||a=='u')
k++;
}
}
if(k>=2)
return 1;
else
return -1;
}}
112.PAN Card
*********************************
public class Main {
public static void main(String[] args) {
String s1="OLE124F";
getvalues(s1);
}
public static void getvalues(String s1) {
if(s1.matches("[A-Z]{3}[0-9]{4}[A-Z]{1}"))
{
System.out.println(1);
}
else
System.out.println(-1);
}
}
113.Password (validatePassword)
**************************
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
String s1=s.next();
boolean b=User.passwordValidation(s1);
if(b==true)
System.out.println("valid password");
else
System.out.println("not a valid password");
}
114.Password Validation
******************************
import java.util.Scanner;
}
public static int display(String password){
if(password.matches(".*[0-9]{1,}.*") &&
password.matches(".*[@#$]{1,}.*") && password.length()>=6 && password.length()<=20)
{
return 1;
}
else
{
return -1;
}
}
115.Pattern Matcher
**************************************
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc= new Scanner (System.in);
String s= sc.nextLine();
System.out.println(matchCharacter(s));
}
return b;
}
}
116.Perfect Number
*************************************************
public class Main {
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;
}
}
{
sb.append('$');
}
}
return sb.toString();
}
}
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1[]={"AAA","BB","CCCC","A","ABCDE"};
String s2[]=new String[s1.length];
for (int i = 0; i < s1.length; i++)
{
s2[i]=s1[i].toLowerCase();
}
Arrays.sort(s2);
for (int i = 0; i < s2.length; i++) {
System.out.println(s2[i]);
}
}
}
120.Prefix Finder
**********************************
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
121.Price Calculator - II
*********************************************
import java.util.*;
public class UserMainCode {
public static void main(String[] args) {
HashMap<String, String> m1=new HashMap<String, String>();
m1.put("monitor", "1200.36");
m1.put("mouse","100.42");
m1.put("speaker", "500.25");
String[] s={"speaker","mouse"};
System.out.println(getTheTotalCostOfPheripherals(m1,s));
}
public static float getTheTotalCostOfPheripherals(HashMap<String,String>
m1,String[] s) {
Float f=(float) 0;
Iterator<String> 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;
}}
122.Programming Logic
*************************************************
import java.util.ArrayList;
123.Proper Case
*******************************************************
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args){
String s1="This is cognizant academy";
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();
}
}
}
}
}
}
}
}
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1="catcowcat";
String s2="catp";
int c=0;
for(int i=0;i<(s1.length()-(s2.length()-1));i++)
{
if(s2.equalsIgnoreCase(s1.substring(i,i+s2.length())))
c++;
}
System.out.println(c);
}
}
131.REMOVE 10'S(removeTens)
**************************
import java.util.Scanner;
public class main
{
public static void main(String[]args)
{
Scanner sc=new Scanner(System.in);
int size=sc.nextInt();
int[]m=new int[size];
int[]n=new int[size];
int j=0;
for(int i=0;i<size;i++)
{
n[i]=sc.nextInt();
}
for(int i=0;i<size;i++)
{
if(n[i]!=10)
{
m[j]=n[i];
j++;
}}
for(int i=0;i<size;i++)
{
System.out.println(m[i]);
}
}
}
for(int i=0;i<al.size();i++)
{
if((i+1)%3!=0)
al2.add(al.get(i));
}
return al2;
}
}
import java.util.*;
int u=a.length;
for(int i=0;i<a.length;i++)
{
if(a[i].length()==m)
u--;
}
return u;
}}
getvalues(hm);
}
public static void getvalues(HashMap<Integer, String> hm) {
int count=0;
//HashMap<Integer, String>hm1=new HashMap<Integer, String>();
Iterator<Integer>itr=hm.keySet().iterator();
while(itr.hasNext())
{
int n=itr.next();
if(n%3!=0)
{
count++;
}
}
System.out.println(count);
}
}
137.Repeat Front(repeatFirstThreeCharacters)
******************************************
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
public class main {
public static void main(String []args){
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
int n=Integer.parseInt(sc.nextLine());
System.out.println(Usermaincode.display(s,n));
}
}
import java.util.StringTokenizer;
public class Usermaincode
{public static String display(String s,int n)
{
StringBuffer sb=new StringBuffer();
StringBuffer sb1=new StringBuffer();
if(s.length()>3)
{ sb.append(s.substring(0,3));
s=sb.toString();
}
for(int i=0;i<n;i++)
sb1.append(s);
return sb1.toString();
}
}
sb.append(s1.substring(s1.length()-n,s1.length()));
}
return sb.toString();
}
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("enter the String:");
String s1=s.nextLine();
int n=s.nextInt();
System.out.println("the lengthiest string is:"+lengthiestString(s1,n));
}
}
139.Retirement (retirementEmployeeList)
**********************************
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Scanner;
public class kape
{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=Integer.parseInt(sc.nextLine());
LinkedHashMap<String,String>a1=new LinkedHashMap<String,String>();
for(int i=0;i<n;i++)
{
a1.put(sc.nextLine(),sc.nextLine());
}
kape1.display(a1);
}
}
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
public class kape1{
public static void display(HashMap<String,String>a1)
{
LinkedHashSet<String>a=new LinkedHashSet<String>();
SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
int y=0;
Iterator<String>it=a1.keySet().iterator();
while(it.hasNext())
{
String s=it.next();
String s1=a1.get(s);
try{
Date d1=sdf.parse(s1);
Calendar c=Calendar.getInstance();
c.setTime(d1);
int y1=c.get(Calendar.YEAR);
int m1=c.get(Calendar.MONTH);
int da1=c.get(Calendar.DAY_OF_MONTH);
Date d2=new Date();
c.setTime(d2);
int y2=c.get(Calendar.YEAR);
int m2=c.get(Calendar.MONTH);
int da2=c.get(Calendar.DAY_OF_MONTH);
y=Math.abs(y1-y2);
if(m1==m2)
{
if(da1>da2)
y--;
}
else if(m1>m2)
y--;
if(y>=60)
a.add(s);
}
catch(Exception e)
{
e.printStackTrace();
}
}
System.out.println(a);
}
}
140.Reverse Split(reshape)
************************
import java.util.Scanner;
import java.util.StringTokenizer;
sb.append(s.charAt(s.length()-1));
for(int i=s.length()-2;i>=0;i--)
{
sb.append('-');
sb.append(s.charAt(i));
}
System.out.print( sb.toString());}}
143.Scores (checkScores)
*************************************************************************
3/1/100/100
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
System.out.println(consecutiveNum(arr, n));
}
break;
}
}
}
return b;
}
144.Sequence in Array
***************************************
public class Main
{
public static void main(String[] args)
{
//int[] a={2,1,4,1,2,3,6,1,2,3};
int[] a={1,2,1,3,4,5,8};
System.out.println(sequenceInArray(a));
}
public static boolean sequenceInArray(int[] a)
{
boolean b = false;
return b;
}
}
145.Sequence Sum
******************************************************
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static int sumOfFibonacci(int n){
int a=0,b=1,c=0,d=1;
for(int i=3;i<=n;i++){
c=a+b;
a=b; b=c;
d=d+c;
}
return d;
}
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n=s.nextInt();
System.out.println(sumOfFibonacci(n));
}
}
import java.util.Scanner;
public class usercc
{
public static void main(String[]args)
{
Scanner sc=new Scanner(System.in);
int size=sc.nextInt();
int[]m=new int[size];
int[]n=new int[size];
int j=0;
for(int i=0;i<size;i++)
{
n[i]=sc.nextInt();
}
for(int i=0;i<size;i++)
{
if(n[i]!=5)
{
m[j]=n[i];
j++;
}}
for(int i=0;i<size;i++)
{
System.out.println(m[i]);
}
}
}
System.out.println(UserMainCode.calculateWordSum(inpList));
}
}
}
}
149.Sorted Array(orderElements)
***************************
import java.util.*;
}
}
150.Start Case
********************************************************
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args){
String s1="Now is the time to act!";
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();
}
}
151.State ID generator
*************************************************************
import java.util.*;
public class UserMainCode
{
public static void main(String[] args)
{
String[] s1={"goa","kerala","gujarat"};
putvalues(s1);
}
public static void putvalues(String[] s1)
{
HashMap<String, String> hm = new HashMap<String, String>();
ArrayList<String> lst1 = new ArrayList<String>();
ArrayList<String> lst2 = new ArrayList<String>();
for(String s : s1)
lst1.add(s.toUpperCase().substring(0,3));
for(String s : s1)
lst2.add(s);
for(int i=0;i<s1.length;i++)
{
hm.put(lst1.get(i),lst2.get(i));
}
//System.out.println(map);
for(Map.Entry<String, String> ans: hm.entrySet())
{
System.out.println(ans.getKey()+":"+ans.getValue());
}
}
}
}
}
import java.util.HashMap;
import java.util.Map;
return (m1.getValue()+"$"+m1.getKey());
}
}
return sa;
}
}
156.String Occurances - II
*******************************************************************************
import java.util.*;
public class UserMainCode {
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
String s1=sc.nextLine();
char []c=s.toCharArray();
char[] c1=s1.toCharArray();
int count=0,result=0;
for(int i=0;i<s.length();i++)
{
for(int j=0;j<s1.length();j++)
{
if(c[i]==c1[j])
{
i++;
count++;
if(i>=s.length())
break;
}
}
if(count==s1.length())
{
result++;
i=i-1;
}
count=0;
}
System.out.print(result);
}}
157.String Occurences
********************************************************************
import java.util.StringTokenizer;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
String s1="ABC xyz AAA";
String s2="w abc";
getvalues(s1,s2);
}
public static void getvalues(String s1, String s2) {
int count=0;
StringTokenizer st=new StringTokenizer(s2," ");
String s3=st.nextToken();
String s4=st.nextToken();
//System.out.println(s4);
StringTokenizer st1=new StringTokenizer(s1," ");
while(st1.hasMoreTokens())
{
String s5=st1.nextToken();
if(s4.equals(s5))
{
count++;
}
}
System.out.println(count);
}
}
int a=0,b=0,c=0;
char c21=s2.charAt(0);
if (Character.isDigit(c21)||Character.isLetter(c21))
{
a=1;
}
if(a==1)
{
char c1=s2.charAt(1);
char c2=s2.charAt(2);
if(c1=='i'){
b=1;
}
if(c2=='x')
{c=1;
}
}
if(a==1&&b==1&&c==1)
{
System.out.print("true");
}else
{
System.out.print("false");
}
}}
}}
}
sb.deleteCharAt(sb.length()-1);
System.out.println(sb);
}
}
}
public static boolean lengthiestString(String s1,String s2,int n){
boolean a=false;
char c=s1.charAt(n);
char d=s2.charAt(s2.length()-n);
String s3=Character.toString(c);
//System.out.println(s3);
String s4=Character.toString(d);
//System.out.println(s4);
if(s3.equalsIgnoreCase(s4))
{
a=true;
}else
{
a=false;
}
return a;
}
}
/**
* @param args
*/
public static void main(String[] args) {
String s1="HelloWorld";
StringBuffer sb=new StringBuffer();
char c=s1.charAt(0);
String s2=s1.substring(1, s1.length()-1);
char c1=s1.charAt(s1.length()-1);
sb.append(c1).append(s2).append(c);
System.out.println(sb);
}
170.String Splitter
****************************************************
import java.util.*;
public class UserMainCode
{
public static void main(String[] args)
{
String ip1="AAA/bba/ccc/DDD";
char ip2='/';
String op[]=loweringCasenReverseofaString(ip1,ip2);
for(String s:op)
System.out.println(s);
}
public static String[] loweringCasenReverseofaString(String ip1, char ip2)
{
StringTokenizer t1 = new StringTokenizer(ip1,"/");
ArrayList<String> lst = new ArrayList<String>();
while(t1.hasMoreTokens())
{
StringBuffer sb = new StringBuffer();
sb.append(t1.nextToken().toLowerCase());
lst.add(sb.reverse().toString());
}
String[] op = new String[lst.size()];
for(int i = 0;i<lst.size();i++)
{
op[i] = (String) lst.get(i);
}
return op;
}
}
}
}
173.Strings Processing(findFruitName)
**********************************
import java.util.Scanner;
}
import java.util.StringTokenizer;
int max=st.countTokens();
String[] ss=new String[max];
while(st.hasMoreElements())
{
ss[i++]=st.nextToken();
}
if(n>max)
h=ss[i-1];
else
h=ss[n-1];
return h;
}
}
}
if(k==0)
{
sum=sum+i;
}
}
for(int r=1; r<=n; r++)
sum1=sum1+r;
return sum1-sum;
}
}
int sum=0;
for(int i=0;i<a.length;i++)
{
for(int j=0;j<b.length;j++)
{if(a[i]==b[j])
sum=sum+a[i];
}}
if(sum==0)
return -1;
else
return sum;
}}
177.Sum of Digits
************************************
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
String s1="abcde";
getvalues(s1);
}
public static void getvalues(String s1) {
int sum=0;
for(int i=0;i<s1.length();i++)
{
char a=s1.charAt(i);
if(Character.isDigit(a))
{
int b=Integer.parseInt(String.valueOf(a));
sum=sum+b;
}
}
if(sum==0)
{
System.out.println(-1);
}
else
System.out.println(sum);
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
{
d=a+b;
}
return d;
}}
{
int sum=0;
for(int i=0;i<n;i++)
sum=(int)(sum+Math.pow(a[i], i));
return sum;
}}}
184.SumOdd (addOddNumbers)
*****************************************************************************
6/9
public class UserMainCode {
public static int sumOf(int n){
int a=0;
for(int i=0;i<=n;i++)
{
if(i%2!=0)
{
a=a+i;
}
}
return a;
}
import java.util.*;
int count=0;
int max = a1.length+a2.length;
ArrayList<Integer> temp = new ArrayList<Integer>(max);
/*union*/
for(int i=0;i<a1.length;i++)
{
if(!temp.contains(a1[i]))
{
++count;
temp.add(a1[i]);
}
}
for(int i=0;i<a2.length;i++)
{
if(!temp.contains(a2[i]))
{
++count;
temp.add(a2[i]);
}
}
union = new int[count];
for(int i=0;i<count;i++)
{
union[i] = (int)temp.get(i);
}
Arrays.sort(union);
/*intersection*/
/*difference */
}
}
187.Test Vowels
*********************************************
public class Main {
public static void main(String[] args) {
String s1="cbisouzze";
System.out.println(vowelsCheck(s1));
}
public static boolean vowelsCheck(String s1) {
boolean b=false;
int n1=0,n2=0,n3=0,n4=0,n5=0;
for(int i=0;i<s1.length();i++){
char c=s1.charAt(i);
if(c=='a')
n1++;
if(c=='e')
n2++;
if(c=='i')
n3++;
if(c=='o')
n4++;
if(c=='u')
n5++;}
if(n1==1 && n2==1 && n3==1 && n4==1 && n5==1)
b=true;
return b;
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
Iterator<Integer> it =hm2.keySet().iterator();
int flag1=0,flag2=0;
while(it.hasNext())
{
int id=it.next();
String name=hm2.get(id);
char[] check=name.toCharArray();
if(check[0]>='a'&&check[0]<='z')
{
if(check[name.length()-1]>='A'&&check[name.length()-1]<='Z')
{
flag1=1;
}
}
for(int i=0;i<name.length();i++)
{
if(check[i]>='0'&&check[i]<='9')
{
flag2=1;
}
}
if(flag1==1&&flag2==1)
{
al2.add(name);
}
flag1=0;
flag2=0;
}
return al2;
}
}
190.7) Triplets(checkTripplets)
***************************
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
int n;
int[] a=new int[10];
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
boolean s=UserMain.checkTripplets(a);
if(s)
System.out.println("TRUE");
else
System.out.println("FALSE");
}
String s1="HOWAREYOU";
kape1.getvalues(s1);
}}
}
}
if(count>0)
{
sb.deleteCharAt(i);
i--;
l--;
}
}
if(sb.length()==0)
{
System.out.println(-1);
}
else
System.out.println(sb.length());
}
}
{
d=2;
}
else if(a==b&&a!=c)
{
d=2;
}
return d;
}}
196.Valid Date
***********************************************
import java.util.ArrayList;
import java.util.Collections;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
String s="12.03.2012";
getvalues(s);
}
public static void getvalues(String s) {
if(s.matches("[0-9]{2}[.]{1}[0-9]{2}[.]{1}[0-9]{4}"))
{
SimpleDateFormat sdf=new SimpleDateFormat("dd.MM.yyyy");
sdf.setLenient(false);
try
{
Date d1=sdf.parse(s);
System.out.println(1);
} catch (ParseException e) {
System.out.println(-1);
}
}
else if(s.matches("[0-9]{2}[/]{1}[0-9]{2}[/][0-9]{4}"))
{
SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
sdf.setLenient(false);
try
{
Date d1=sdf.parse(s);
System.out.println(1);
} catch (ParseException e) {
System.out.println(-1);
}
}
else if(s.matches("[0-9]{2}[-]{1}[0-9]{2}[-][0-9]{4}"))
{
SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy");
sdf.setLenient(false);
try
{
Date d1=sdf.parse(s);
System.out.println(1);
} catch (ParseException e) {
System.out.println(-1);
}
}
else
System.out.println(-1);
}
}
197.Validate Number (validateNumber)
**************************************** -94923
public class Usermaincode
{
public static int display(String s)
{
int count=0,n;
for(int i=1;i<s.length();i++)
{
char c=s.charAt(i);
if(Character.isDigit(c))
count++;
}if(count==s.length()-1)
{n=Integer.parseInt(s);
n=(-1)*n;
}
else
n=-1;
return n;
}
}
Date d2=sdf1.parse(str);
System.out.println("Valid time");
}
catch(Exception e)
{
System.out.println("Invalid time");
}
}
else
{
SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
sdf.setLenient(false);
try
{
Date d1=sdf.parse(str);
System.out.println("Valid time");
}
catch(Exception e){
System.out.println("Invalid time");
}
}
}
String s1=name;
int n1=0,n2=0,n3=0,n4=0,n5=0;
for(int i=0;i<s1.length();i++){
char c=s1.charAt(i);
if(c=='a' || c=='A')
n1++;
if(c=='e' || c=='E')
n2++;
if(c=='i' || c=='I')
n3++;
if(c=='o' || c=='O')
n4++;
if(c=='u' || c=='U')
n5++;}
if(n1==1 && n2==1 && n3==1 && n4==1 && n5==1)
return 1;
else
return 0 ;
}
202.Vowel Count
****************************************
import java.util.*;
public class PO
{public static void main(String[]args){
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
int max=0;
int count=0;
for(int i=0;i<s.length();i++)
{
char c=s.charAt(i);
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||
c=='I'||c=='O'||c=='U')
{
count++;
}
}
if(count>max)
{
max=count;
}
System.out.print(max);}}
203.Vowels (storeMaxVowelWord)
*************************
import java.util.StringTokenizer;
public class B {
public static void main(String[] args) {
String s1 = "What is your name?";
getvalues(s1);
}
public static void getvalues(String s1) {
int i = 0;
StringTokenizer st = new StringTokenizer(s1," ");
int len = 0;
int count = 0;
int count2 = 0;
String s6 = null;
while (st.hasMoreTokens()) {
String s5 = st.nextToken();
len = s5.length();
count=0;
for (i = 0; i < len; i++) {
if (s5.charAt(i) == 'a' || s5.charAt(i) == 'e'|| s5.charAt(i) == 'i' ||
s5.charAt(i) == 'o'|| s5.charAt(i) == 'u'
||s5.charAt(i) == 'A' ||s5.charAt(i) == 'E' ||s5.charAt(i) == 'I' ||s5.charAt(i) ==
'O' ||s5.charAt(i) == 'U')
count++;
}
if (count > count2)
{
count2 = count;
s6 = s5;
}
}
System.out.println(s6);
}
}
}
ArrayList<String> arr=new ArrayList<String>();
arr=UserMainCode.getName(str);
Iterator<String> it=arr.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
import java.util.*;
public class B
{
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++)
{
String s = s1[i];
for(int j = 0;j<s.length();j++)
{
Character c = s.charAt(j);
if(Character.isDigit(c))
{
sum+=Integer.parseInt(s.valueOf(c));
}
}
}
System.out.println(sum);
}
}