This program output multiple elements per line from an array.
Sample Question:
Declare an array myList with 40 elements of type int. Then initialized the first 20 elements of the array with the square of the index variable. Then the last 20 elements of the array value is initialized to four times the index variable. Output five elements per line.
Sample Code:
public class ArrayMyList
{
public static void main(String[] args)
{
int arraySize = 40;
int lineToOutput = 5;
int[] myList = new int[arraySize];
for(int i = 0; i < arraySize; i++)
{ if(i < 20)
myList[i] = i * i;
else
myList[i] = i * 4;
if((i + 1) % lineToOutput == 0)
System.out.print(array[i]+ "\n");
else
System.out.print(array[i]+ "\t");
}
}
}
Subscribe to:
Post Comments (Atom)
2 comments:
wowooo...
da mcm assigment then post kt blog da..hee..
love flash...say no to programming..
hehe..
شركة تنظيف مكيفات
شركة جلي بلاط
مهندس افران
شركة انشاء مسابح
Post a Comment