Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Wednesday, 23 November 2016

Java interview Program to find second Smallest number in an integer array by sorting the elements.

class SecondSmallestNumber{

public static void main(String args[])
{

int numbers[] = {6,3,37,12,46,5,64,21};

  Arrays.sort(numbers);

  System.out.println("Smallest Number: "+numbers[0]);
  System.out.println("Second Smallest Number: "+numbers[1]);

 }

}

Java interview Program to find second smallest number in an integer array without sorting the elements.
class SecondSmallestNumber{

int[] x ={10,11,12,13,14,6,3,-1};

        int small=x[0];

 for(int i=0;i<x.length;i++)
 {
        if(x[i]<small)
        {
        small=x[i];
        }
 }

   int sec_Small=x[0];

for(int i=0;i<x.length;i++)
 {
        if(x[i]<sec_Small && x[i]!=small)
        {
        sec_Small=x[i];
        }
  }

        System.out.println("Second Smallest Number: "sec_Small);
        }
}

Program to print prime numbers in java


public class primenumbers {

public static void main(String[] args) {

int num=50;
int count=0;

for(int i=2;i<=num;i++){

count=0;

for(int j=2;j<=i/2;j++){

if(i%j==0){
count++;
break;
}

}

if(count==0){

System.out.println(i);

}

}

}

}