Question: How can I get this loop that will add all the values in all odd columns, and print their total aligned below the column. I
How can I get this loop that will add all the values in all odd columns, and print their total aligned below the column.
I need that the 45 gets aligned with the 9, 135 with 27, 225 with 45, 315 with 63, and 105 with 81.
This is the code:
public class wherever
{
//arrays method.
public static void arrays(int _2array[][])
{
//variable to store the sum of all the elements of the matrix.
int sum =0;
//Run the loop to insert the
//values into the 2-D array.
//Display the value present at the
//array index after being updated.
for(int i=0;i
{
for (int j=0;j
{
_2array[i][j] = i*j;
//Use printf() method
//to align the values.
System.out.printf("%5d",_2array[i][j]);
//Update the value of the variable
//sum by adding the value of the
//present at the current index
//of the 2-d array to it.
sum = sum + _2array[i][j];
}
//Move to the next line
//after displaying each row.
System.out.printf(" ");
}
//variable to store the sum of each odd column.
int col_sum=0;
System.out.print("Total");
//Run the loop to find the
//sum of each odd column.
//Display the sum of each column
for(int i=0;i
{
if(i%2!=0)
{
for(int j=0;j
{
col_sum = col_sum + _2array[j][i];
}
//Display the sum of each odd column.
System.out.printf("%10d",col_sum);
//Reset the value of the variable col_sum.
col_sum=0;
}
}
System.out.printf(" Array Index Total:" + sum + " " );
}
//main method.
public static void main(String args[])
{
//Define a 2-D array of int type.
int _2array[][] = new int[10][10];
//Call the arrays() method.
arrays(_2array);
}
}
i.Problems @Javadoc ? Declaration?Consolex
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
