Java Articles

Page 14 of 450

How to clear screen using Java?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 3K+ Views

Following is the code to clear screen using Java −Examplepublic class Demo{    public static void main(String[] args){       System.out.print("\033[H\033[2J");       System.out.flush();    } }OutputThe screen would be clearedA class named Demo contains the main function. Here, the ANSI escape code is written, that clears the screen. The flush function resets the cursor to the top of the window screen.

Read More

Java Program for Counting Sort

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 275 Views

The Counting Sort counts the number of objects having distinct key values. Let us see an example −Note − The below code can be used with negative numbers as well.Exampleimport java.util.*; public class Demo{    static void count_sort(int[] arr){       int max_val = Arrays.stream(arr).max().getAsInt();       int min_val = Arrays.stream(arr).min().getAsInt();       int range = max_val - min_val + 1;       int count[] = new int[range];       int result[] = new int[arr.length];       for (int i = 0; i < arr.length; i++){          count[arr[i] - min_val]++;   ...

Read More

Java Program for Comb Sort

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 322 Views

The Comb Sort in Java eliminates the smaller values situated to the end of the list and the inversions are removed oby by one. Let us see an example −Exampleimport java.util.Arrays; public class Demo{    void comb_sort(int nums[]){       int len_gap = nums.length;       float shrink_val = 1.3f;       boolean swap = false;       while (len_gap > 1 || swap) {          if (len_gap > 1) {             len_gap = (int)(len_gap / shrink_val);          }          swap = ...

Read More

Java Program for Anagram Substring Search

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 281 Views

Following is an example for Anagram Substring Search in Java −Examplepublic class Demo{    static final int max_val = 256;    static boolean compare_vals(char my_arr_1[], char my_arr_2[]){       for (int i = 0; i < max_val; i++)       if (my_arr_1[i] != my_arr_2[i])       return false;       return true;    }    static void search_subs(String my_pattern, String my_text){       int pat_len = my_pattern.length();       int txt_len = my_text.length();       char[] count_pat = new char[max_val];       char[] count_txt = new char[max_val];       for (int ...

Read More

Java Program for Bitonic Sort

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 339 Views

In Bitonic Sort, the comparision is in predefined sequence (Bitonic sequence), not dependent on the data to be sorted. Let us see an example for Bitonic Sort Java program −Examplepublic class Demo{    void compare_swap(int my_arr[], int i, int j, int direction){       if ((my_arr[i] > my_arr[j] && direction == 1) || (my_arr[i] < my_arr[j] && direction == 0)){          int temp = my_arr[i];          my_arr[i] = my_arr[j];          my_arr[j] = temp;       }    }    void merge_vals(int my_arr[], int low, int cnt, int direction){   ...

Read More

String Formatting in Java using %

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 220 Views

Followimg is the code to implement String formatting in Java using % −Examplepublic class Demo {    public static void main(String args[]){       String my_str = " sample.";       String concat_Str = String.format("This is a" + "%s", my_str);       String format_str_1 = String.format("The value is %.4f", 78.92367);       System.out.println(concat_Str);       System.out.println(format_str_1);    } }OutputThis is a sample. The value is 78.9237A class named Demo contains the main function. Here a string value is defined, which is used to format the string, by concatenating it to another variable. Similarly, a floating ...

Read More

Java Program to find largest prime factor of a number

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 3K+ Views

Following is the Java code to find the largest prime factor of a number −Exampleimport java.io.*; import java.util.*; public class Demo{    static long maxPrimeFactors( long val){       long max_prime = -1;       while (val % 2 == 0) {          max_prime = 2;          val >>= 1;       }       for (int i = 3; i 2)       max_prime = val;       return max_prime;    }    public static void main(String[] args){       int val = 148592;   ...

Read More

Java Program to find the perimeter of a cylinder

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 313 Views

Following is the Java code to find the perimeter of a cylinder −Exampleimport java.io.*; public class Demo{    static int find_peri(int dia, int ht){       return 2*(dia + ht);    }    public static void main(String[] args){       int dia = 7;       int ht = 15;       System.out.println("The perimeter of the cylinder is " + find_peri(dia, ht) + " units");    } }OutputThe perimeter of the cylinder is 44 unitsA class named Demo defines a static function that takes in two values, diameter and height. This function computes the sum of ...

Read More

Java Program to find the vertex, focus and directrix of a parabola

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 237 Views

Following is the Java program to find the vertex, focus and directrix of a parabola −Examplepublic class Demo{    public static void find_values(float val_1, float val_2, float val_3){       System.out.println("The value of vertex is (" + (-val_2 / (2 * val_1)) + ", "+ (((4 * val_1 * val_3) - (val_2 * val_2)) / (4 * val_1)) + ")");       System.out.println("The value of focus is (" + (-val_2 / (2 * val_1)) + ", " + (((4 * val_1 * val_3) - (val_2 * val_2) + 1) / (4 * val_1)) + ")");       ...

Read More

Printing Integer between Strings in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ Views

Following is the Java program to print integer between Strings −Examplepublic class Demo{    public static void main(String[] args){       System.out.println("The equals symbol is present between two integer values ");       System.out.println(45+5 + "=" +(56+11));       System.out.println(45+5 + " equals symbol " +(56+11));    } }OutputThe equals symbol is present between two integer values 50=67 50 equals symbol 67A class named Demo contains the main function that prints integer values between two strings.

Read More
Showing 131–140 of 4,495 articles
« Prev 1 12 13 14 15 16 450 Next »
Advertisements