Found 7442 Articles for Java

Java program to subtract two matrices

Shriansh Kumar
Updated on 13-Sep-2024 15:54:00

1K+ Views

For two given matrix each of size m×n, write a Java program to subtract them. The matrix has a row and column arrangement of its elements. A matrix with m rows and n columns can be called as m×n matrix. Individual entries in the matrix are called element and can be represented by a[i][j] which means that the element a is present at the ith row and jth column. Note that two matrix can be subtracted if and only if the number of rows and columns of each matrix are equal. Now, let's understand the problem statement with an example ... Read More

Java program to subtract two matrices

Shriansh Kumar
Updated on 13-Sep-2024 15:54:00

1K+ Views

For two given matrix each of size m×n, write a Java program to subtract them. The matrix has a row and column arrangement of its elements. A matrix with m rows and n columns can be called as m×n matrix. Individual entries in the matrix are called element and can be represented by a[i][j] which means that the element a is present at the ith row and jth column. Note that two matrix can be subtracted if and only if the number of rows and columns of each matrix are equal. Now, let's understand the problem statement with an example ... Read More

Find the 3rd smallest number in a Java array.

Kumar Varma
Updated on 12-Mar-2020 10:10:40

5K+ Views

ExampleFollowing is the required program.Live Demopublic class Tester {    public static int getThirdSmallest(int[] a) {       int temp;       //sort the array       for (int i = 0; i < a.length; i++) {          for (int j = i + 1; j < a.length; j++) {             if (a[i] > a[j]) {                temp = a[i];                a[i] = a[j];                a[j] = temp;             }          }       }       //return third smallest element       return a[2];    }    public static void main(String args[]) {       int a[] = { 11,10,4, 15, 16, 13, 2 };       System.out.println("Third Smallest: " +getThirdSmallest(a));    } }OutputThird smallest: 10

Find the 3rd smallest number in a Java array.

Kumar Varma
Updated on 12-Mar-2020 10:10:40

5K+ Views

ExampleFollowing is the required program.Live Demopublic class Tester {    public static int getThirdSmallest(int[] a) {       int temp;       //sort the array       for (int i = 0; i < a.length; i++) {          for (int j = i + 1; j < a.length; j++) {             if (a[i] > a[j]) {                temp = a[i];                a[i] = a[j];                a[j] = temp;             }          }       }       //return third smallest element       return a[2];    }    public static void main(String args[]) {       int a[] = { 11,10,4, 15, 16, 13, 2 };       System.out.println("Third Smallest: " +getThirdSmallest(a));    } }OutputThird smallest: 10

Factorial program in Java without using recursion.

Vikyath Ram
Updated on 18-Jun-2020 08:09:48

2K+ Views

Following is the required program.ExampleLive Demopublic class Tester {    static int factorial(int n) {       if (n == 0)          return 1;       else          return (n * factorial(n - 1));    }    public static void main(String args[]) {       int i, fact = 1;       int number = 5;       fact = factorial(number);       System.out.println(number + "! = " + fact);    } }Output5! = 120

Factorial program in Java without using recursion.

Vikyath Ram
Updated on 18-Jun-2020 08:09:48

2K+ Views

Following is the required program.ExampleLive Demopublic class Tester {    static int factorial(int n) {       if (n == 0)          return 1;       else          return (n * factorial(n - 1));    }    public static void main(String args[]) {       int i, fact = 1;       int number = 5;       fact = factorial(number);       System.out.println(number + "! = " + fact);    } }Output5! = 120

Prime number program in Java.

V Jyothi
Updated on 18-Jun-2020 07:00:54

2K+ Views

Following is the required program.ExampleLive Demopublic class Tester {    public static void main(String args[]) {       int i, m = 0, flag = 0;       int n = 41;// it is the number to be checked       m = n / 2;       if (n == 0 || n == 1) {          System.out.println(n + " not a prime number");       } else {          for (i = 2; i

Prime number program in Java.

V Jyothi
Updated on 18-Jun-2020 07:00:54

2K+ Views

Following is the required program.ExampleLive Demopublic class Tester {    public static void main(String args[]) {       int i, m = 0, flag = 0;       int n = 41;// it is the number to be checked       m = n / 2;       if (n == 0 || n == 1) {          System.out.println(n + " not a prime number");       } else {          for (i = 2; i

Fibonacci series program in Java using recursion.

V Jyothi
Updated on 05-Mar-2020 12:16:09

2K+ Views

Following is the required program.ExampleLive Demopublic class Tester {    static int n1 = 0, n2 = 1, n3 = 0;    static void fibbonacci(int count) {       if (count > 0) {          n3 = n1 + n2;          n1 = n2;          n2 = n3;          System.out.print(" " + n3);          fibbonacci(count - 1);       }    }    public static void main(String args[]) {       int count = 5;       System.out.print(n1 + " " + n2);       fibbonacci(count - 2);    } }Output0 1 1 2 3

Fibonacci series program in Java using recursion.

V Jyothi
Updated on 05-Mar-2020 12:16:09

2K+ Views

Following is the required program.ExampleLive Demopublic class Tester {    static int n1 = 0, n2 = 1, n3 = 0;    static void fibbonacci(int count) {       if (count > 0) {          n3 = n1 + n2;          n1 = n2;          n2 = n3;          System.out.print(" " + n3);          fibbonacci(count - 1);       }    }    public static void main(String args[]) {       int count = 5;       System.out.print(n1 + " " + n2);       fibbonacci(count - 2);    } }Output0 1 1 2 3

Advertisements