puja
BAN USERSystem.out.println("enter the numbers");
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
String str = br.readLine();
//System.out.println(str);
String[] arr_str = str.split(" ");
int[] arr_i= new int[arr_str.length];
int count =0;
for(String x: arr_str)
{
int i = Integer.parseInt(x);
arr_i[count]=i;
count++;
}
//sort the integer array
for(int a=0; a<arr_str.length; a++)
{
for(int b=1; b<(arr_str.length-a); b++)
{
if(arr_i[b-1]>arr_i[b])
{
int temp = arr_i[b-1];
arr_i[b-1]=arr_i[b];
arr_i[b]=temp;
}
}
}
//now calculate the average of all the integers except the two largest ones
int sum=0;
for(int num=0; num<(arr_str.length-3); num++)
{
sum=sum+arr_i[num];
}
int average_sum = sum/2;
System.out.println(average_sum);
public class SmallestNumberInArray
{
public static void main(String[] arg)
{
int[] arr = {-1,10,-5,-10};
int len_arr = arr.length;
int smallest_num = arr[0];
for(int i=1; i<len_arr;i++)
{
if(arr[i]<smallest_num)
{
smallest_num=arr[i];
}
}
System.out.println("smallest number is "+smallest_num);
}
}
In java it can be done like this:
String str ="Hello I am going to I with Hello am";
String[] arr_str = str.split(" ");
int len =arr_str.length;
//sort the string
for(int i=0; i<len; i++)
{
for(int j=1; j<(len-i); j++)
{
if(arr_str[j-1].compareToIgnoreCase(arr_str[j])>0)
{
String temp = arr_str[j-1];
arr_str[j-1]=arr_str[j];
arr_str[j]=temp;
}
}
}
//now count the number of times the words are repeated
try{
for(int a=0; a<len; a++)
{
int count =1;
if(arr_str[a].equals(arr_str[a+1]))
{
count++;
System.out.println(arr_str[a]+" is repeated "+count+" times" );
}
}
}
catch(Exception e){}
}
- puja July 27, 2012