1. java program a. A for loop to print the numbers: 1 to 10 on one line separate
ID: 3594356 • Letter: 1
Question
1. java program
a. A for loop to print the numbers: 1 to 10 on one line separated by a space
b. A while loop to print the numbers: 10 to 1 on one line separated by a space
2. Hand-write a loop to print the even numbers from 1 to 15 that are divisible by 6
3. Hand-write code to declare a two dimensional array of 3 columns and 4 rows. The Array will hold doubles.
What is the array initialized to by default?
4. Define the following arrays using an initializer list:
a. 1 2 3
2 3 4
5 6 7
b. 1 2
4 5 6
c. 1 3 4 5 6
5. Write code to print the arrays in Problem 4
6. Given the two dimensional array below:
public class Class1 {
public static void main (String [] args) {
int[][] list = { {9, 8}, {6, 5}, {3, 2}, {7, 7} };
} }
a. What is the value of list.length _______________
b. What is the value of list[0].length _______________
c. Hand-write a loop to print every element of the array, list, (4 rows and 2 columns of output)
d. Hand-write a loop to print every element of the second row of the Array
e. Hand-write a loop to print every element of the first column of the Array
f. How would you sum all the elements of the Array? Hand-write a loop to do so.
g. How do you find the max or the min in the array? Hand-write a loop to do so.
h. How do you sum the elements of a column of an array? Hand-write a loop to do so.
i. How do you sum the elements of a row of an array? Hand-write a loop to do so.
Explanation / Answer
class Main{
public static void main(String[] args)
{
// from 1 to 15
for(int i=1;i<=15;i++)
{
// even numbers
if(i%2 == 0)
{
// divisible by 6
if(i%6 == 0)
{
System.out.print(i+" ");
}
}
}
}
}
/*SAMPLE OUTPUT
6 12
*/
class Main{
public static void main(String[] args)
{
double a[][] = new double[4][3];
for(int i=0;i<4;i++)
{
for(int j=0;j<3;j++)
System.out.print(a[i][j]+" ");
System.out.println();
}
}
}
/*
Initially array holds all zeros
SAMPLE OUTPUT
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
*/
public static void main(String[] args)
{
for(int i=1;i<=10;i++)
{
System.out.print(i+" ");
}
}
}
/*SAMPLE OUTPUT
1 2 3 4 5 6 7 8 9 10
*/ 1.b class Main{
public static void main(String[] args)
{
int i=1;
while(i<=10)
{
System.out.print(i+" ");
i++;
}
}
}
/*SAMPLE OUTPUT
1 2 3 4 5 6 7 8 9 10
*/ 2
class Main{
public static void main(String[] args)
{
// from 1 to 15
for(int i=1;i<=15;i++)
{
// even numbers
if(i%2 == 0)
{
// divisible by 6
if(i%6 == 0)
{
System.out.print(i+" ");
}
}
}
}
}
/*SAMPLE OUTPUT
6 12
*/
class Main{
public static void main(String[] args)
{
double a[][] = new double[4][3];
for(int i=0;i<4;i++)
{
for(int j=0;j<3;j++)
System.out.print(a[i][j]+" ");
System.out.println();
}
}
}
/*
Initially array holds all zeros
SAMPLE OUTPUT
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.