PROGRAM TO PRINT AND CALCULATE SUM OF RIGHT/SECONDARY DIAGONAL ELEMENTS OF MATRIX IN FOUR DIFFERENT LOGIC BY ICSE X STUDENTS
PROGRAM TO PRINT AND CALCULATE SUM OF RIGHT/SECONDARY DIAGONAL ELEMENTS OF MATRIX IN FOUR DIFFERENT LOGIC BY SCHOOL STUDENTS
import java.util.Scanner;
public class RightDiagonal
{
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("Enter the number of rows and columns");
int r=s.nextInt();
int c=s.nextInt();
int a[][]=new int[r][c];
System.out.println("Enter the Elements of matrix ");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
a[i][j]=s.nextInt();
}
}
/* Logic No. 1
for(int i=0;i<r;)
{
for(int j=c-1;j>=0;j--,i++)
{
System.out.println(a[i][j]);
sum=sum+a[i][j];
}
}
*/
/* OR Logic no. 2
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.println(a[i][j]);
sum=sum+a[i][c-1-i];
}
}
*/
/*OR Logic No. 3
int a=0,b=c-1;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
sum=sum+a[a][b];
a++; b--;
}
}
*/
/*Logic No. 4
j=n-1;
for(int i=0;i<r;i++)
{
sum=sum+a[i][j];
j--;
}
*/
System.out.println("sum="+sum);
}
}
Comments
Post a Comment