Found 338 Articles for Java Programming

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

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 binary search

Lakshmi Srinivas
Updated on 13-Mar-2020 05:52:42

2K+ Views

Binary search is a fast search algorithm with run-time complexity of Ο(log n). This search algorithm works on the principle of divide and conquer. For this algorithm to work properly, the data collection should be in the sorted form.The binary search looks for a particular item by comparing the middle most item of the collection. If a match occurs, then the index of the item is returned. If the middle item is greater than the item, then the item is searched in the sub-array to the left of the middle item. Otherwise, the item is searched for in the sub-array to the right ... Read More

Java program to implement insertion sort

karthikeya Boyini
Updated on 13-Mar-2020 05:49:39

689 Views

This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which is always sorted. For example, the lower part of an array is maintained to be sorted. An element which is to be inserted in this sorted sub-list has to find its appropriate place and then it has to be inserted there. Hence the name, insertion sort.The array is searched sequentially and unsorted items are moved and inserted into the sorted sub-list (in the same array).Algorithm1.If it is the first element, it is already sorted. return 1; 2.Pick next element 3.Compare with all elements in the sorted sub-list ... Read More

Java program to implement selection sort

Arjun Thakur
Updated on 13-Mar-2020 05:44:07

3K+ Views

Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list.The smallest element is selected from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array. This process continues moving unsorted array boundary from one element to the right.Algorithm1.Set MIN to location 0 2.Search the minimum element in the ... Read More

Java program to print the factorial of the given number

Ankith Reddy
Updated on 13-Mar-2020 05:36:50

4K+ Views

Factorial of a positive integer n is the product of all values from n to 1. For example, the factorial of 3 is (3 * 2 * 1 = 6). Algorithm1. Take integer variable A 2. Assign a value to the variable 3. From value, A up to 1 multiply each digit and store 4. The final stored value is factorial of AExampleimport java.util.Scanner;    public class Factorial {       public static void main(String args[]){          int i, factorial=1, number;          System.out.println("Enter the number to which you need to find the factorial:");          Scanner sc = new Scanner(System.in);          number = sc.nextInt();          for(i = 1; i

Java program to print a Fibonacci series

Samual Sam
Updated on 13-Mar-2020 05:27:25

2K+ Views

Fibonacci Series generates subsequent number by adding two previous numbers. Fibonacci series starts from two numbers − F0 & F1. The initial values of F0 & F1 can be taken 0, 1 or 1, 1 respectively.Fn = Fn-1 + Fn-2Algorithm1. Take integer variable A, B, C 2. Set A = 1, B = 1 3. DISPLAY A, B 4. C = A + B 5. DISPLAY C 6. Set A = B, B = C 7. REPEAT from 4 - 6, for n timesExampleLive Demopublic class FibonacciSeries2{    public static void main(String args[]) {       int a, b, c, i, n;       n = 10;       a = b = 1;       System.out.print(a+" "+b);       for(i = 1; i

StringTokenizer class in Java

V Jyothi
Updated on 05-Mar-2020 12:18:39

413 Views

The StringTokenizer class of the java.util package allows an application to break a string into tokens.This class is a legacy class that is retained for compatibility reasons although its use is discouraged in new code.Its methods do not distinguish among identifiers, numbers, and quoted strings.This class methods do not even recognize and skip comments.ExampleLive Demoimport java.util.*;   public class Sample {    public static void main(String[] args) {         // creating string tokenizer       StringTokenizer st = new StringTokenizer("Come to learn");         // checking next token       System.out.println("Next token is : " + st.nextToken());   }     }OutputNext token is : Come

Advertisements