Está en la página 1de 8

LAB ASSIGNMENT 1

1. Write a java program to find the largest numbers in a given list of array.
2. Write a java program to find the sum of any two matrices.
3. Write a program to display

1 2

1 2 3

1 2 3 4
SOURCE CODE
import java.util.Scanner;

class qn1{

Scanner input = new Scanner(System.in);

private int n, max = 0;

private int[] A = new int[20];

public void getData(){

//get value for array elements in qn1

System.out.println("Enter the number of elements to be inserted into the array: ");

n = input.nextInt();

for(int i =0;i<n;i++){

System.out.println("Enter the element a[" +i+"]");

A[i] = input.nextInt();

public void displayData(){

//print the maximum value in the array

for(int i =0;i<n;i++){

for(int j =i+1;j<n;j++){

if(A[i]>A[j]){

max = A[i];

else{

max = A[j];

}
}

System.out.println("The largest value in the array is "+max+"\n");

class qn2{

Scanner input = new Scanner(System.in);

private int[][] A = new int[2][2];

private int[][] B = new int[2][2];

private int[][] C = new int[2][2];

public void getData(){

//get value for matrices in question no2.

System.out.println("Enter Information for Matrix A");

for(int i=0;i<2;i++){

for(int j =0;j<2;j++){

System.out.println("Enter element for A["+i+"]["+j+"] : ");

A[i][j] = input.nextInt();

System.out.println("Enter Information for Matrix B");

for(int i=0;i<2;i++){

for(int j =0;j<2;j++){
System.out.println("Enter element for B["+i+"]["+j+"] : ");

B[i][j] = input.nextInt();

private void sum(){

for(int i=0;i<2;i++){

for(int j =0;j<2;j++){

C[i][j] = A[i][j] + B[i][j];

public void displaySum(){

sum();

System.out.println("The sum of matrices A and B is : ");

for(int i=0;i<2;i++){

for(int j =0;j<2;j++){

System.out.print(C[i][j]+"\t");

System.out.println("\n");

}
class qn3{

int i,j;

public void displayData(){

for(i=1;i<5;i++){

for(j=1;j<=i;j++){

System.out.print("\t"+j);

System.out.println("\n");

public class assignment1 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int qnA = 0;

System.out.println("Enter the question number for which the answer is to be displayed: ");

qnA = input.nextInt();

if(qnA ==1){

qn1 obj = new qn1();

obj.getData();

obj.displayData();
}

else if(qnA==2){

qn2 obj = new qn2();

obj.getData();

obj.displaySum();

else if(qnA==3){

qn3 obj = new qn3();

obj.displayData();

else{

System.out.println("Error!\n");

}
OUTPUT

También podría gustarte