Thursday, July 27, 2017

Sum Numbers From A String


This simple but tricky programs is to filter numbers from a given string and find the sum of all those numbers.





public class StringFilter {

    public static void main(String[] args)
         {  
                 String s = "aB1cD2";
                 int sum=0;       
                 String s1=s.toLowerCase();
                 for(int i=0;i<s1.length();i++)
                 {
                          int m=Character.getNumericValue(s1.charAt(i));
                          if(m >=0  && m<=9)
                          {
                                sum += m;
                          }       
               
                  }
       
        System.out.println(s1+" :- "+sum);
    }

}



Output :  ab1cd2 :- 3

Friday, March 24, 2017

Sum Of Subset of an Array

Program to find sum of subset of a given array and match it with the given sum, if its match with the sum it will print "Yes", if not match with the given sum print "No" if sum of subset is negative number print "error".

Exp :
1
4 8
1
3
5
3

1 is the number of test case, 4 is size of array , 8 is the given sum , 
output : Yes


Sum Of Subset of an Array

Thursday, March 23, 2017

Print biggest numbers from the Array

This is the basic program to print biggest number from the array, but if in the right side of the array is big number from our current number skip it from printing ,else in the right no number is bigger then current number so print it.

Ex : 3,5203,3,6,7,8,1,2,23,54,232,554,879,0

in this array we need to start comparing numbers from 3 and we will go till 0, if we found any bigger number then 3 in right side we will skip it and move to next element.

Output : 5203,879,0

it printing 0 also because there is no number in the right side of 0 so its the biggest.

Print biggest numbers from the Array