0% found this document useful (0 votes)
2 views9 pages

Arrays in Java

The document contains three simple Java programs demonstrating operations on arrays. The first program handles a single-dimensional array to calculate the sum of its elements, while the second and third programs perform addition and subtraction on two-dimensional arrays, respectively. Each program prompts the user for input and displays the results of the operations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Arrays in Java

The document contains three simple Java programs demonstrating operations on arrays. The first program handles a single-dimensional array to calculate the sum of its elements, while the second and third programs perform addition and subtraction on two-dimensional arrays, respectively. Each program prompts the user for input and displays the results of the operations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Simple java program on single dimensional array

import [Link].*;

import [Link].*;

class Sarray

public static void main(String args[])

int[] a=new int[5];

int i=0;

int sum=0;

[Link]("enter array values");

Scanner s=new Scanner([Link]);

for(i=0;i<=4;i++)

a[i]=[Link]();

for(i=0;i<=4;i++)

[Link]("i value is"+a[i]);

for(i=0;i<=4;i++)

sum=sum+a[i];
}

[Link]("sum is"+sum);

Output:
1. Simple java program on array addition

import [Link].*;

import [Link].*;

class Madd

public static void main(String args[])

int[][] a=new int[4][2];

int[][] b=new int[4][2];

int[][] c=new int[4][2];

int i=0;

[Link]("enter A array values");

Scanner s=new Scanner([Link]);

for(i=0;i<=3;i++)

a[i][0]=[Link]();

a[i][1]=[Link]();

[Link]("enter B array values");

for(i=0;i<=3;i++)

b[i][0]=[Link]();
b[i][1]=[Link]();

[Link]("the A array values are");

for(i=0;i<=3;i++)

[Link](+a[i][0]);

[Link](+a[i][1]);

[Link]("the B array values are");

for(i=0;i<=3;i++)

[Link](+b[i][0]);

[Link](+b[i][1]);

for(i=0;i<=3;i++)

c[i][0]=a[i][0]+b[i][0];

c[i][1]=a[i][1]+b[i][1];

[Link]("the sum of array values are");

for(i=0;i<=3;i++)
{

[Link](+c[i][0]);

[Link](+c[i][1]);

}
3 Simple java program on array subtraction

import [Link].*;

import [Link].*;

class Subarray

public static void main(String args[])

int[][] a=new int[4][2];

int[][] b=new int[4][2];


int[][] c=new int[4][2];

int i=0;

[Link]("enter A array values");

Scanner s=new Scanner([Link]);

for(i=0;i<=3;i++)

a[i][0]=[Link]();

a[i][1]=[Link]();

[Link]("enter B array values");

for(i=0;i<=3;i++)

b[i][0]=[Link]();

b[i][1]=[Link]();

[Link]("the A array values are");

for(i=0;i<=3;i++)

[Link](+a[i][0]);

[Link](+a[i][1]);

}
[Link]("the B array values are");

for(i=0;i<=3;i++)

[Link](+b[i][0]);

[Link](+b[i][1]);

for(i=0;i<=3;i++)

c[i][0]=a[i][0]-b[i][0];

c[i][1]=a[i][1]-b[i][1];

[Link]("the diff of array values are");

for(i=0;i<=3;i++)

[Link](+c[i][0]);

[Link](+c[i][1]);

You might also like