Found 74 Articles for Java Technologies

Java program to calculate the product of two numbers

Samual Sam
Updated on 13-Mar-2020 07:07:49

8K+ Views

The * operator in Java is used to multiply two numbers. Read required numbers from the user using Scanner class and multiply these two integers using the * operator.Exampleimport java.util.Scanner; public class MultiplicationOfTwoNumbers {    public static void main(String args[]){       Scanner sc = new Scanner(System.in);       System.out.println("Enter the value of the first number ::");       int a = sc.nextInt();       System.out.println("Enter the value of the first number ::");       int b = sc.nextInt();       int result = a*b;       System.out.println("Product of the given two numbers ... Read More

Java program to reverse a string using recursion

Ankith Reddy
Updated on 13-Mar-2020 07:00:25

778 Views

Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function. You can reverse a string using the recursive function as shown in the following program.ExampleLive Demopublic class StringReverse {    public String reverseString(String str){           if(str.isEmpty()){          return str;       } else {          return reverseString(str.substring(1))+str.charAt(0);       }    }    public static void main(String[] args) {       StringReverse obj = new StringReverse();       String result = obj.reverseString("Tutorialspoint");       System.out.println(result);    } }OutputtniopslairotuT

Java program to find the average of given numbers using arrays

Lakshmi Srinivas
Updated on 13-Mar-2020 06:56:23

3K+ Views

You can read data from the user using scanner class.Using the nextInt() method of this class get the number of elements from the user.Create an empty array.Store the elements entered by the user in the array created above.Finally, Add all the elements in the array and divide the sub by the number of elements.Exampleimport java.util.Scanner; public class AverageUsingArrays {    public static void main(String args[]){       //Reading total no.of elements       Scanner sc = new Scanner(System.in);       System.out.println("Enter the number of elements/numbers");       int num = sc.nextInt();       ... Read More

Java program to Print Odd and Even Number from an Array

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

953 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

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

Ankith Reddy
Updated on 13-Mar-2020 06:05:34

8K+ 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

11K+ 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

49K+ 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 13-Mar-2020 05:58:21

9K+ 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

Java program to implement linear search

Chandu yadav
Updated on 13-Mar-2020 05:51:22

5K+ Views

Linear search is a very simple search algorithm. In this type of search, a sequential search is done for all items one by one. Every item is checked and if a match is found then that particular item is returned, otherwise the search continues till the end of the data collection.Algorithm1.Get the length of the array. 2.Get the element to be searched store it in a variable named value. 3.Compare each element of the array with the variable value. 4.In case of a match print a message saying element found. 5.else, print a message saying element not foundExampleLive Demopublic class ... Read More

Java program to implement bubble sort

Samual Sam
Updated on 19-Jun-2020 13:56:12

699 Views

Bubble sort is a simple sorting algorithm. This sorting algorithm is a comparison-based algorithm in which each pair of adjacent elements is compared and the elements are swapped if they are not in order. This algorithm is not suitable for large datasets as its average and worst case complexity is of Ο(n2) where n is the number of items.ExampleLive Demopublic class BubbleSort {    static void bubbleSort(int[] arr) {       int n = arr.length;       int temp = 0;       for(int i = 0; i < n; i++) {         ... Read More

Advertisements