Found 9150 Articles for Object Oriented Programming

Java Program to find the perimeter of a cylinder

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

273 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

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

184 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

Alshifa Hasnain
Updated on 19-Feb-2025 17:50:35

556 Views

In this article, we will learn to write a Java program to calculate the sum of cubes of the first n natural numbers. Understanding the Cube Sum Formula The sum of cubes of the first n natural numbers follows a mathematical formula − S = 13 + 23 + 33 + ... + n3 = { ( n ( n + 1 ) ) / 2 }2 Different Approaches Following are the two different approaches to printing the cube sum of first n natural numbers − Using a Loop Using a ... Read More

Java Program for Common Divisors of Two Numbers

AmitDiwan
Updated on 18-Nov-2024 22:33:06

707 Views

In this article, we will learn to find the common divisors of two numbers using Java. The program will use a recursive method to calculate the greatest common divisor (GCD) of the two numbers, and then determine how many divisors are shared by both numbers. The output will display the total number of common divisors. Problem Statement Write a Java program to find and count the common divisors of two given numbers. Below is a demonstration of the same − Input val_1= 68 val_2= 34 Output The common divisors between the two numbers is4 Steps to find the common divisors ... Read More

Java Program for Bitonic Sort

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

275 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

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

Java Program for Anagram Substring Search

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

234 Views

Following is an example for Anagram Substring Search in Java −Example Live Demopublic 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 ... Read More

Java Program for Comb Sort

AmitDiwan
Updated on 04-Jul-2020 09:41:16

261 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 −Example Live Demoimport 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 Counting Sort

AmitDiwan
Updated on 04-Jul-2020 09:39:15

209 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.Example Live Demoimport 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

Advertisements