UncleCoder.com

UncleCoder.com

Free programming examples and instructions

C program to find the sum of border elements of an array

c program for how to find the sum of border elements of an array

by Krishna viswambharan


Posted on 23 Oct 2017 Category: C Views: 122

Edited on 07 May 2018


C program to find the sum of border elements of an array

In this article, we will see how to find the sum of border elements of an array using C program. C code for the computation is given below:

#include <stdio.h>
#include <conio.h>
void main()
{
     int a[25][25],i,j,r,c,sum=0;
     printf(“Enter the row size :”);
     scanf(“%d”,&r);
     printf(“Enter the coloumn size :”);
     scanf(“%d”,&c);
     printf(“Enter the array elements :”);
     for(i=0;i<r;i++)
     {
          for(j=0;j<c;j++)
          {
              scanf(“%d”,&a[i][j]);
          }
     }
     for(i=0;i<r;i++)
     {
          for(j=0;j<c;j++)
          {
              if((i==0||i==(r-1))||(j==0||j==(c-1)))
                       sum=sum+a[i][j];
          }
     }
     printf(“\n Sum of border elements =%d”,sum);
     getch();
}

In this program we have taken a 3x3 matrix that is row size is 3 and column size is also 3. The code works as follows:-

  1. Read the row and column
  2. Read the array elements(matrix elements)
  3. Check ((i==0||i==(r-1))||(j==0||j==(c-1))), if so

                  3.1) set sum=sum+a[i][j],till j<c

       4. Increment j

       5.Repeat the steps 3 to 4 till i<r, i++

       6. Print sum

output



Leave a Comment:


Advertisement