Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Java Articles
Page 18 of 450
String Formatting in Java using %
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 MoreJava Program to find largest prime factor of a number
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 MoreJava Program to find the perimeter of a cylinder
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 MoreJava Program to find the vertex, focus and directrix of a parabola
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 MorePrinting Integer between Strings in Java
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 MorePrinting Triangle Pattern in Java
Following is the Java program to print Triangle pattern −Exampleimport java.util.*; public class Demo{ public static void main(String[] args){ Scanner my_scan = new Scanner(System.in); System.out.println("Enter the number of rows which needs to be printed"); int my_row = my_scan.nextInt(); for (int i = 1; i = i; j--){ System.out.print(" "); } for (int j = 1; j
Read MoreJava Program for Largest K digit number divisible by X
Following is the Java program for largest K digit number divisible by X −Exampleimport java.io.*; import java.lang.*; public class Demo{ public static int largest_k(int val_1, int val_2){ int i = 10; int MAX = (int)Math.pow(i, val_2) - 1; return (MAX - (MAX % val_1)); } public static void main(String[] args){ int val_1 = 25; int val_2 = 2; System.out.println("The largest 2 digit number divisible by 25 is "); System.out.println((int)largest_k(val_1, val_2)); } }OutputThe largest 2 digit ...
Read MoreJava Program for Reversal algorithm for array rotation
Following is the Java program to implement Reversal algorithm for array rotation −Exampleimport java.io.*; public class Demo{ static void rotate_left(int my_arr[], int no_of_rotation){ int n = my_arr.length; array_reversal(my_arr, 0, no_of_rotation - 1); array_reversal(my_arr, no_of_rotation, n - 1); array_reversal(my_arr, 0, n - 1); } static void array_reversal(int my_arr[], int start, int end){ int temp; while (start < end) { temp = my_arr[start]; my_arr[start] = my_arr[end]; my_arr[end] = temp; ...
Read MoreMax Heap in Java
Max heap is a complete binary tree, wherein the value of a root node at every step is greater than or equal to value at the child node.Below is an implementation of Max Heap using library functions.Exampleimport java.util.*; public class Demo{ public static void main(String args[]){ PriorityQueue my_p_queue = new PriorityQueue(Collections.reverseOrder()); my_p_queue.add(43); my_p_queue.add(56); my_p_queue.add(99); System.out.println("The elements in the priority queue are : "); Iterator my_iter = my_p_queue.iterator(); while (my_iter.hasNext()) System.out.println(my_iter.next()); my_p_queue.poll(); ...
Read MoreMerge two sets in Java
To merge two sets in Java, the code is as follows −Exampleimport java.util.stream.*; import java.util.*; import java.io.*; public class Demo{ public static Set set_merge(Set set_1, Set set_2){ Set my_set = set_1.stream().collect(Collectors.toSet()); my_set.addAll(set_2); return my_set; } public static void main(String[] args){ Set my_set_1 = new HashSet(); my_set_1.addAll(Arrays.asList(new Integer[] { 34, 67, 89, 102 })); Set my_set_2 = new HashSet(); my_set_2.addAll(Arrays.asList(new Integer[] { 77, 11, 0 , -33})); System.out.println("The first set contains " + ...
Read More