Found 9326 Articles for Object Oriented Programming

Java program to find if the given number is a leap year?

Chandu yadav
Updated on 07-Nov-2023 02:58:02

64K+ Views

Finding a year is a leap or not is a bit tricky. We generally assume that if a year number is evenly divisible by 4 is a leap year. But it is not the only case. A year is a leap year if −1. It is evenly divisible by 1002. If it is divisible by 100, then it should also be divisible by 4003. Except this, all other years evenly divisible by 4 are leap years.Algorithm1. Take integer variable year2. Assign a value to the variable3. Check if the year is divisible by 4 but not 100, DISPLAY "leap year"4. ... Read More

Java program to print prime numbers below 100

Samual Sam
Updated on 13-Mar-2020 06:59:36

717 Views

Any whole number which is greater than 1 and has only two factors that is 1 and the number itself, is called a prime number. Other than these two number it has no positive divisor. For example −7 = 1 × 7Few prime numbers are − 1, 2, 3, 5, 7, 11 etc.Algorithm1. Take integer variable A2. Divide the variable A with (A-1 to 2)3. If A is not divisible by any value (A-1 to 2), except itself it is a prime number.4. Repeat this for all the numbers starting from 2 to the required limit.ExampleLive Demopublic class First100Primes {    public static void main(String args[]){       for(int i = 2; i

Java program to print the transpose of a matrix

karthikeya Boyini
Updated on 13-Mar-2020 06:16:50

2K+ Views

The transpose of a matrix is the one whose rows are columns of the original matrix, i.e. if A and B are two matrices such that the rows of the matrix B are the columns of the matrix A then Matrix B is said to be the transpose of Matrix A.To print the transpose of the given matrix −Create an empty matrix.Copy the contents of the original matrix to the new matrix such that elements in the [j][i] position of the original matrix should be copied to the [i][j] position of the new matrix.Print the new matrix.ExampleLive Demopublic class TransposeSample{ ... Read More

Java program to Print Odd and Even Number from an Array

Ankith Reddy
Updated on 13-Mar-2020 06:18:02

1K+ Views

In the loop check, the result of I %2 operation on each element, if 0 the element is even else the element is odd.ExampleLive Demopublic class OddNumbersInAnArray {    public static void main(String args[]){       int[] myArray = {23, 93, 56, 92, 39};       System.out.println("Even numbers in the given array are:: ");       for (int i=0; i

How to write Java program to add two matrices

Samual Sam
Updated on 13-Mar-2020 06:15:21

5K+ Views

To add two matrices −Create an empty matrixAt each position in the new matrix, assign the sum of the values in the same position from the given two matrices i.e. if A[i][j] and B[i][j] are the two given matrices then, the value of c[i][j] should be A[i][j] + B[i][j]ExampleLive Demopublic class AddingTwoMatrices{    public static void main(String args[]){       int a[][]={{1,2,3},{4,5,6},{7,8,9}};       int b[][]={{1,1,1},{1,1,1},{1,1,1}};       int c[][]=new int[3][3];       for(int i = 0;i

Java program to find the smallest number in an array

Lakshmi Srinivas
Updated on 13-Mar-2020 06:10:15

13K+ Views

To find the smallest element of the given array, first of all, sort the array.Sorting an arrayCompare the first two elements of the arrayIf the first element is greater than the second swap them.Then, compare 2nd and 3rd elements if the second element is greater than the 3rd swap them.Repeat this till the end of the array.After sorting an array print the 1st element of the array.ExampleLive Demopublic class SmallestNumberInAnArray {    public static void main(String args[]){       int temp, size;       int array[] = {10, 20, 25, 63, 96, 57};       size = array.length;       for(int i = 0; i

Java program to find the 2nd smallest number in an array

Ankith Reddy
Updated on 21-Jun-2024 11:21:08

9K+ Views

To find the 2nd smallest element of the given array, first of all, sort the array.Sorting an arrayCompare the first two elements of the arrayIf the first element is greater than the second swap them.Then, compare 2nd and 3rd elements if the second element is greater than the 3rd swap them.Repeat this till the end of the array.After sorting an array print the 2nd element of the array.ExampleLive Demopublic class SmallestNumberInAnArray {    public static void main(String args[]){       int temp, size;       int array[] = {10, 20, 25, 63, 96, 57};       size = array.length;       for(int i = 0; i

Java program to find the largest number in an array

karthikeya Boyini
Updated on 13-Mar-2020 06:02:57

12K+ Views

To find the largest element of the given array, first of all, sort the array.Sorting an arrayCompare the first two elements of the arrayIf the first element is greater than the second swap them.Then, compare 2nd and 3rd elements if the second element is greater than the 3rd swap them.Repeat this till the end of the array.After sorting an array print the 1st element from the end of the array.ExampleLive Demopublic class ThirdLargestNumberInAnArray {    public static void main(String args[]){       int temp, size;       int array[] = {10, 20, 25, 63, 96, 57};       size = array.length;       for(int i = 0; i

Java program to find the 2nd largest number in an array

Samual Sam
Updated on 02-Sep-2023 15:09:09

55K+ Views

To find the second largest element of the given array, first of all, sort the array.Sorting an arrayCompare the first two elements of the arrayIf the first element is greater than the second swap them.Then, compare 2nd and 3rd elements if the second element is greater than the 3rd swap them.Repeat this till the end of the array.After sorting an array print the second element from the end of the array.ExampleLive Demopublic class ThirdLargestNumberInAnArray {    public static void main(String args[]){       int temp, size;       int array[] = {10, 20, 25, 63, 96, 57};       size = array.length;       for(int i = 0; i

Java program to find the 3rd largest number in an array

Arjun Thakur
Updated on 31-May-2024 17:50:35

10K+ Views

To find the third largest number of the given array, first of all, sort the array.Sorting an arrayCompare the first two elements of the arrayIf the first element is greater than the second swap them.Then, compare 2nd and 3rd elements if the second element is greater than the 3rd swap them.Repeat this till the end of the array.After sorting an array print the third element from the end of the array.ExampleLive Demopublic class ThirdLargestNumberInAnArray {    public static void main(String args[]){    int temp, size;    int array[] = {10, 20, 25, 63, 96, 57};    size = array.length;        for(int i = 0; i

Advertisements