java - how to find a number in a range in array -
for example if enter inrange(1,6)
must print {2,2,2,3,5}
below array. not sure if logic right. there better way this? not sure of how construct return statement. want without using arraylist or array.
public class agecount { static int[] a=new int[]{1,2,45,6,3,2,1,2,5,6,65,45,43,21,34,34}; public agecount(){ } public static int inrange(int b,int e) { int store[]=new int[a.length]; int count=0; (int = 0; < a.length; i++) { if(a[i]>b && a[i]<e) { return a[i]; } } return 0; }
if method has print numbers of given range, doesn't have return :
public static void inrange(int b,int e) { int count=0; system.out.print('{'); boolean first = true; (int = 0; < a.length; i++) { if(a[i]>=b && a[i]<=e) { if (!first) { system.out.print(','); } else { first = false; } system.out.print(a[i]); } } system.out.print('}'); }
this assuming order of output doesn't matter. if matter, sort input array prior loop.
Comments
Post a Comment