You are on page 1of 5

//sum of two numbers

import java.util.Scanner; /

public class MyClass {

public static void main(String[] args) {

int x, y, sum;

Scanner myObj = new Scanner(System.in);

System.out.println("Type a number:");

x = myObj.nextInt();

System.out.println("Type another number:");

y = myObj.nextInt();

sum = x + y;

System.out.println("Sum is: " + sum);

OUTPUT:

ava -cp /tmp/JAay88znOD MyClass

Type a number:5

Type another number:

13

Sum is: 18
QUESTION 2

/find smallest number

import java.util.Scanner;

public class Exercise1 {

public static void main(String[] args)

Scanner in = new Scanner(System.in);

System.out.print("Enter first number: ");

double x = in.nextDouble();

System.out.print("Enter Second number: ");

double y = in.nextDouble();

System.out.print("Enter third number: ");

double z = in.nextDouble();

System.out.print("The smallest value is " + smallest(x, y, z)+"\n" );

public static double smallest(double x, double y, double z)

return Math.min(Math.min(x, y), z);

OUTPUT:

java -cp /tmp/pPursjaDG4 Exercise1

Enter first number: 5

Enter Second number: 6

Enter third number: 7

The smallest value is 5.0


QUESTION 3 :

//transepose of matrix

public class Transpose {

public static void main(String[] args) {

int row = 2, column = 3;

int[][] matrix = { {2, 3, 4}, {5, 6, 4} };

display(matrix);

int[][] transpose = new int[column][row];

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

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

transpose[j][i] = matrix[i][j];

}
display(transpose);

public static void display(int[][] matrix) {

System.out.println("The matrix is: ");

for(int[] row : matrix) {

for (int column : row) {

System.out.print(column + " ");

System.out.println();

OUTPUT:

java -cp /tmp/hK4IrfiMO8 Transpose

The matrix is:

2 3 4

5 6 4

The matrix is:

2 5 3 6

4 4

java -cp /tmp/hK4IrfiMO8 Transpose

The matrix is:

2 3 4

5 6 4

The matrix is:

2 5

3 6

4 4

You might also like