Found 2617 Articles for Java

Java Program for GCD of more than two (or array) numbers

AmitDiwan
Updated on 04-Jul-2020 10:41:27

434 Views

Following is the Java program for GCD of more than two numbers −Example Live Demopublic class Demo{    static int gcd_of_nums(int val_1, int val_2){       if (val_1 == 0)       return val_2;       return gcd_of_nums(val_2 % val_1, val_1);    }    static int find_gcd(int arr[], int no){       int result = arr[0];       for (int i = 1; i < no; i++){          result = gcd_of_nums(arr[i], result);          if(result == 1){             return 1;          }   ... Read More

Java Program for focal length of a spherical mirror

AmitDiwan
Updated on 04-Jul-2020 10:05:38

212 Views

Following is the Java code for focal length of a spherical mirror −Example Live Demoimport java.util.*; import java.lang.*; public class Demo{    public static float concave_f_len(float val){       return val/2 ;    }    public static float convex_f_len(float val){       return - (val/2 ) ;    }    public static void main(String argc[]){       float val = 55 ;       System.out.print("The focal length of spherical mirror (concave) is : " + concave_f_len(val) + "units");       System.out.println("The focal length of spherical mirror (convex) is : "+ convex_f_len(val) + "units");    } }OutputThe ... Read More

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

AmitDiwan
Updated on 04-Jul-2020 10:03:43

128 Views

Following is the Java program to find the vertex, focus and directrix of a parabola −Example Live Demopublic 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

Java Program to find the perimeter of a cylinder

AmitDiwan
Updated on 04-Jul-2020 10:01:17

182 Views

Following is the Java code to find the perimeter of a cylinder −Example Live Demoimport 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 ... Read More

Java Program to find largest prime factor of a number

AmitDiwan
Updated on 04-Jul-2020 09:59:52

2K+ Views

Following is the Java code to find the largest prime factor of a number −Example Live Demoimport 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

String Formatting in Java using %

AmitDiwan
Updated on 04-Jul-2020 09:57:44

121 Views

Followimg is the code to implement String formatting in Java using % −Example Live Demopublic 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 ... Read More

Java Program to cube sum of first n natural numbers

AmitDiwan
Updated on 04-Jul-2020 09:55:23

399 Views

Following is the Java code to cube sum of first n natural numbers −Example Live Demoimport java.util.*; import java.lang.*; public class Demo{    public static int first_n_nat_no(int val){       int ini_sum = 0;       for (int x=1; x

Java Program for Common Divisors of Two Numbers

AmitDiwan
Updated on 04-Jul-2020 09:52:34

508 Views

Following is an example for Common Divisors of Two numbers in Java −Example Live Demopublic class Demo{    static int find_gcd(int val_1, int val_2){       if (val_1 == 0)       return val_2;       return find_gcd(val_2%val_1,val_1);    }    static int common_divisors(int val_1,int val_2){       int no = find_gcd(val_1, val_2);       int result = 0;       for (int i=1; i

Java Program for Bitonic Sort

AmitDiwan
Updated on 04-Jul-2020 09:50:29

142 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 −Example Live Demopublic 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

Java Program for Binary Search (Recursive)

AmitDiwan
Updated on 04-Jul-2020 09:47:14

5K+ Views

Following is the program for Recursive Binary Search in Java −Example Live Demopublic class Demo{    int rec_bin_search(int my_arr[], int left, int right, int x){       if (right >= left){          int mid = left + (right - left) / 2;          if (my_arr[mid] == x)          return mid;          if (my_arr[mid] > x)          return rec_bin_search(my_arr, left, mid - 1, x);          return rec_bin_search(my_arr, mid + 1, right, x);       }       return -1;    } ... Read More

Advertisements