Write a program to create an array containing the numbers in the given range.
Input (From, To) |
Output (Integer Array) |
10, 20 |
{10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} |
-6, 4 |
{-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4} |
2, 9 |
{2, 3, 4, 5, 6, 7, 8, 9} |
class CreateNumbersInRangeArray
{
public static void main(String s[])
{
int result[] = createNumbersInRangeArray(10, 20);
for(int i = 0; i < result.length; i++)
{
System.out.print(result[i] + " ");
}
}
public static int[] createNumbersInRangeArray(int start, int end)
{
}
}
Topic:
Learn Arrays And Loops
If you need explanation Read this topic
If you need Answer Take test on this topic
User comments below. All of them might not be correct.
here we have to create an array wid elements in a given range specified as start and end.... i.e start will be the firstelement of the string and end will be the last element of the strhing.. now coming to the length of the array so the lenvth of the aarrray will be end-start 1 .....array.length=end-start (plus)1.....nowfor storing elemts in array we have tp frstly initialise a variable p=0 wich will traverse thru every cell of array... now take a loop i=start to end.... den inside this loop array[p+ + plusplus]=I; THIS STMT WILL STORE EVERY ELEMNT FRM STRT TO END IN TH ARRAY.. NOWEXIT LOOP AMD RETURN ARRAY
Posted by Asma Mujtaba Khan 2014-12-22 06:10:10
Int d;
d=(start-end)+1;
Int a[d];
a[0]=start;
For(i=1;i<d;i++)
a[i]=a[i-1]+1;
return a[];
Posted by Sai Abhishek 2014-12-22 06:14:48
--create array with size (end-start)+1
int res[]=new int[(end-start)+1];
--Use FOR loop starting from start till end with another counter to refer array index
for(int i=start,j=0;i<=end;i++,j++)
--assign re[j]=i
--at the end return array
return res
Posted by Mânïshå Mùlchåndânï 2014-12-22 10:51:19
*create new Array int newArray=new int[end-start+1]
*start for loop for inserting elements and take an variable for indexes let us take i=0 bcz Index start from 0..
* put condition as start<=end in loop...increment start in every iteration..
*insert element into array
newArray[i++]=start;
*after coming out of the loop return array
Posted by Uday Kumar 2014-12-22 13:13:52
This dose is now closed and the winners are Mânïshå Mùlchåndânï, for 'First Correct Comment', Uday Kumar, for 'Second Correct Comment'. The 'lucky liker' is Butt Sabzar. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.
Posted by Merit Campus 2014-12-23 05:00:01