×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Sum of the diagonal elements of 2D Array Pronic Number - java
Sum of the Border elements of 2D Array - Java
8612    Arnab De    04/12/2018

create a double dimension array of size m x n, where m and n are input by the user and calculate the sum of the border elements.

Border elements of an array are placed at the border of the array. Which are satisfy some conditions (i)if row no = 0 or (total row -1) (ii) column no = 0 or (total col -1). Therefore, we just travel the array once and add all the elements which meet the above conditions.

softetechnologies
import java.util.*;
public class Border2D
{
    public static void main(String []args)
    {
        Scanner sc=new Scanner(System.in);
        int s=0,m,n;
        System.out.print("Enter the Row No :");
        m=sc.nextInt();
        System.out.print("Enter the Col No :");
        n=sc.nextInt();
        
        int [][]arr=new int[m][n];
        
        for(int r=0;r<m;r++)
        {
            for(int c=0;c<n;c++)
            {
                arr[r][c]=sc.nextInt();
                
            }
        }
        for(int r=0;r<m;r++)
        {
            for(int c=0;c<n;c++)
            {
                if((r==0) || (r==(m-1)) || (c==0) || (c==(n-1)))  
                {
                    s+=arr[r][c];
                }
            }
        }
        System.out.println("Sum Is : " + s);
    }
}
softetechnologies
Sum of the diagonal elements of 2D Array Pronic Number - java
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
No Comment Found Yet.
Sri Sri Ramakrishna Paramahamsa
Do yourself what you wish others to do.
Sri Sri Ramakrishna Paramahamsa
27429
52.76
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     42032
01/01/2018     36003
28/06/2017     34089
25/06/2018     33447
02/08/2017     32401
01/08/2017     26867
06/07/2017     26791
15/05/2017     26418
14/07/2017     21855
21/04/2018     20717
softetechnologies