Search This Blog

Showing posts with label Program. Show all posts
Showing posts with label Program. Show all posts

Wednesday, 9 April 2014

java program to convert char array to String

java program to convert char array to String



public class ConvertCharArrayToString {
       public static void main(String[] args) {
              char[] charArr = { 'w''e''l''c','o','m','e',' ','R''a',' j' };

              String str = new String(charArr);
              System.out.println("converted String is:" + str);

              String str1 = String.valueOf(charArr);
              System.out.println("covnverted String is:" + str1);
       }
}

OUTPUT
converted String is:welcome Raj
covnverted String is:welcome Raj

java program to convert String to char array

java program to convert String to char array


public class ConvertStringToCharArray {
       public static void main(String[] args) {
              String str="welcome Raj";
              char[] charArr=str.toCharArray();

              for(char temp:charArr){
                     System.out.println(temp);
              }
       }
}

OUTPUT
w
e
l
c
o
m
e

R
a
j

Find Minimum and Maximum value of Array in Java


Find Minimum and Maximum value of Array in Java


public class FindMinAndMaxValueInArray {
       public static void main(String[] args) {
              int arr[] = { 69, 23, 47, 81, 92, 88, 52, 48, 56, 66, 65, 76, 71, 85,
                           49, 53, 56, 61, 65, 84 };
              findMaxValue(arr);
              findMinValue(arr);
       }

       public static void findMinValue(int[] arr) {
              int min = arr[0];
              for (int i = 0; i < arr.length; i++) {
                     if (min > arr[i]) {
                           min = arr[i];
                     }
              }
              System.out.println("minimum value of the array is:" + min);
       }

       public static void findMaxValue(int[] arr) {
              int max = arr[0];
              for (int i = 0; i < arr.length; i++) {
                     if (max < arr[i]) {
                           max = arr[i];
                     }
              }
              System.out.println("maximum value of array is:" + max);

       }
}
output
maximum value of array is:92
minimum value of the array is:23

Sunday, 6 April 2014

Find Duplicates in an array

Find Duplicates in an array


package com.blogspot.rajvishwakarma15;

public class CheckDuplictesInArray {
       public static void main(String[] args) {
              String[] strArr = new String[] { "a""b""c""s""d""e""a",
                           "b""s" };
              detectDuplicates(strArr);
       }

       public static void detectDuplicates(String[] arr) {

              for (int i = 0; i < arr.length; i++) {
                     for (int j = i + 1; j < arr.length; j++) {
                           if (arr[j] == arr[i]) {
                      System.out.println("Duplicate found! Original was at location "
                               + (i + 1) + " and match is at " + (j + 1));
                                  System.out.println("Duplicated value is:" + arr[j]);
                           }
                     }
              }

       }
}


OUTPUT
Duplicate found! Original was at location 1 and match is at 7 Duplicated value is:a

Duplicate found! Original was at location 2 and match is at 8 Duplicated value is:b

Duplicate found! Original was at location 4 and match is at 9 Duplicated value is:s

Java Program To Find Second Largest Number in Array

public class SecondLargestNumberInArray {
       public static void main(String[] args) {
              int arr[] = { 18, 23, 77, 81, 92, 88, 52, 48, 26, 66, 65, 76, 11, 85,
                           99, 53, 26, 61, 65, 34 };
              secondLargeNumber(arr);
       }

       public static void secondLargeNumber(int[] arr) {
              int largest = arr[0];
              int secondLargest = arr[0];
              for (int i = 0; i < arr.length; i++) {
                     if (arr[i] > largest) {
                           secondLargest = largest;
                           largest = arr[i];
                          
                     } else if (arr[i] > secondLargest) {
                           secondLargest = arr[i];
                         
                     }
              }
              System.out.println("second largest in array is:" + secondLargest);

       }
}


OutPut:

Second Largest in array is: 92


Wednesday, 2 April 2014

Write a program to find sum of each digit in the given number using recursion.

Write a program to find sum of each digit in the given number using recursion.


Description:
Below example shows how to find out sum of each digit in the given number using recursion logic. For example, if the number is 259, then the sum should be 2+5+9 = 16.


package com.blogspot.rajvishwakarma18;
public class MyNumberSumRec {
    int sum = 0;
     
    public int getNumberSum(int number){
         
        if(number == 0){
            return sum;
        } else {
            sum += (number%10);
            getNumberSum(number/10);
        }
        return sum;
    }
     
    public static void main(String a[]){
        MyNumberSumRec mns = new MyNumberSumRec();
        System.out.println("Sum is: "+mns.getNumberSum(223));
    }
}

Output:
Sum is: 7