0% found this document useful (0 votes)
80 views

Name: Ali Akbar Ejaz

This document contains the details of an assignment submitted by Ali Akbar Ejaz, a 5th semester Bscs student with registration number Bscs10171013. The assignment contains code solutions in Java for 2 questions - 1) Using a for-each loop to print a 2D array and 2) Printing column values of a 2D array. It also contains code solutions for matrix addition, subtraction and multiplication operations. The assignment was submitted to professor Numan Sahib.

Uploaded by

Ali Akbar
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)
80 views

Name: Ali Akbar Ejaz

This document contains the details of an assignment submitted by Ali Akbar Ejaz, a 5th semester Bscs student with registration number Bscs10171013. The assignment contains code solutions in Java for 2 questions - 1) Using a for-each loop to print a 2D array and 2) Printing column values of a 2D array. It also contains code solutions for matrix addition, subtraction and multiplication operations. The assignment was submitted to professor Numan Sahib.

Uploaded by

Ali Akbar
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
You are on page 1/ 13

Name : Ali Akbar Ejaz

Class : Bscs 5th semester

Reg.no : Bscs10171013

Assignment : java

Submitted To : Numan sahb


Q.No.1
2-d Array by using for each function?
package foreachloop;

import java.util.Scanner;

/**
*
* @author Ali Chishti
*/
public class Foreachloop {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
{
Scanner in=new Scanner(System.in);
int a[][]=new int[3][3];
int i=0,j=0;
//inserting elements into 2D array
while(i<a.length)
{
j=0;
while(j<a[0].length)
{
System.out.print("Enter value : ");
a[i][j++]=in.nextInt();
}
i++;
}
//printing 2D array using for-each loop
for(int arr[] : a)
{
for(int val : arr)
{
System.out.print(val+" ");
}
System.out.println();
}
}
}

Q.N0.3
Matrix of multiplication, Addition and subtraction?
Addition:
package additionmatrix;

import java.util.Scanner;

/**
*
* @author Ali Chishti
*/
public class Additionmatrix {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
{
int m, n, c, d;
Scanner in = new Scanner(System.in);

System.out.println("Enter the number of rows and columns of


matrix");
m = in.nextInt();
n = in.nextInt();

int first[][] = new int[m][n];


int second[][] = new int[m][n];
int sum[][] = new int[m][n];

System.out.println("Enter the elements of first matrix");

for (c = 0; c < m; c++)


for (d = 0; d < n; d++)
first[c][d] = in.nextInt();
System.out.println("Enter the elements of second matrix");

for (c = 0 ; c < m ; c++)


for (d = 0 ; d < n ; d++)
second[c][d] = in.nextInt();

for (c = 0; c < m; c++)


for (d = 0; d < n; d++)
sum[c][d] = first[c][d] + second[c][d]; //replace '+' with '-' to
subtract matrices

System.out.println("Sum of the matrices:");

for (c = 0; c < m; c++)


{
for (d = 0; d < n; d++)
System.out.print(sum[c][d]+"\t");

System.out.println();
}
}
}
}

Subtraction:
package subtractionmatrix;

import java.util.Scanner;

/**
*
* @author Ali Chishti
*/
public class Subtractionmatrix {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
{
int i, j;
int mat1[][] = new int[3][3];
int mat2[][] = new int[3][3];
int mat3[][] = new int[3][3];
Scanner scan = new Scanner(System.in);

System.out.print("Enter Matrix 1 Elements : ");


for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
mat1[i][j] = scan.nextInt();
}
}

System.out.print("Enter Matrix 2 Elements : ");


for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
mat2[i][j] = scan.nextInt();
}
}

System.out.print("Subtracting Matrices (i.e. Matrix1 -


Matrix2)...\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
mat3[i][j] = mat1[i][j] - mat2[i][j];
}
}

System.out.print("Result of Matrix1 - Matrix2 is :\n");


for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
System.out.print(mat3[i][j]+ " ");
}
System.out.println();
}
}
}

}
Multiplication:
package multiplematrix;

/**
*
* @author Ali Chishti
*/
public class Multiplematrix {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,3,3}};
int c[][]=new int[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}

Q.No2
Column wise value in 2-d array?
package columnarray;

/**
*
* @author Ali Chishti
*/
public class Columnarray {
private static Object[] array;

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int[][] values =
{
{ 10, 20, 30 }, { 40, 50, 60 }, { 70, 80, 90 },
{ 11, 21, 31 }
};

for (int row = 0; row < 4; row++)


{
for (int col = 0; col < 3; col++)
{
System.out.print(values[row][col] + " ");
}
System.out.println(); // Print new line.
}
}

You might also like