Advertisements
Advertisements
प्रश्न
Write a program to accept an integer double dimensional array and find the sum of its border and the sum of its non border elements separately.
कोड लेखन
Advertisements
उत्तर
import java.util.*;
public class DDA3
{
void main()
{
Scanner sc = new Scanner(System.in);
int ar[ ][ ] = new int[4][5];
int r, c;
System.out.println("Filling the array: ");
for(r = 0; r < 4; r++)
{
for(c = 0; c < 5; c++)
{
//System.out.print("Cell[" + r+"]["+ c +"] : " );
ar[r][c] = (int)(Math.random() * 100);
}
}
System.out.println("The array is : ");
for(r = 0; r < 4; r++)
{
for(c = 0; c < 5; c++)
{
System.out.print(ar[r][c]+"\t");
}
System.out.println();
}
int sumBorder = 0, sumNonBorder = 0, sumAll = 0;
System.out.println("The calculation of elements : ");
for(r = 0; r < 4; r++)
{
for(c = 0; c < 5; c++)
{
sumAll = sumAll + ar[r][c];
if(r == 0 || r == 3)
{
System.out.print(" + " + ar[r][c]);
sumBorder = sumBorder + ar[r][c];
}
if((c=0 && r!= 0 &&r !=3) ||(c=4&&r!= 0 && r != 3 ))
}
System.out.print(" + " + ar[r][c]);
sumBorder = sumBorder + ar[r][c];
}
}
}
sumNonBorder = sumAll - sumBorder;
System.out.println("The Sum of all elements : " + sumAll);
System.out.println("The Sum of Border elements : " + sumBorder);
System.out.println("The Sum of Non Border elements : " + sumNonBorder);
}
}
Output:
Filling the array:
The array is :
17 21 50 72 45
96 79 31 66 68
1 55 94 77 88
50 80 97 75 11
The Sum of elements:
+ 17+21 + 50 + 72 + 45 + 96 + 68 + 1 + 88 + 50 + 80+97 + 75 + 11 The Sum
of all elements: 1173
The Sum of Border elements : 771
The Sum of Non Border elements : 402
shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
