Found 7442 Articles for Java

Java Program to Find the closest pair from two sorted arrays

AmitDiwan
Updated on 08-Jul-2020 12:03:33

340 Views

To find the closest pair from two sorted array, the Java code is as follows −Example Live Demopublic class Demo {    void closest_pair(int my_arr_1[], int my_arr_2[], int arr_1_len, int arr_2_len, int sum){       int diff = Integer.MAX_VALUE;       int result_l = 0, result_r = 0;       int l = 0, r = arr_2_len-1;       while (l=0){          if (Math.abs(my_arr_1[l] + my_arr_2[r] - sum) < diff){             result_l = l;             result_r = r;             ... Read More

Java program to find sum of even factors of a number

AmitDiwan
Updated on 05-Sep-2024 11:22:18

922 Views

In this article we’ll find the sum of even factors of a given number using Java. We’ll start by checking if the number is even, then identify all of its factors, sum up those that are even, and finally display the result. Problem Statement Write a Java program to find the sum of even factors of a number. Below is the demostration of the same − Input num=16 Ouput The sum of even factors of the number is 30 Steps to find sum of even factors of a number Following are the steps to find sum of even factors of a number ... Read More

Java Program to find reminder of array multiplication divided by n

AmitDiwan
Updated on 08-Jul-2020 11:58:16

234 Views

To find reminder of array multiplication divided by n, the Java code is as follows −Example Live Demoimport java.util.*; import java.lang.*; public class Demo{    public static int remainder(int my_arr[], int arr_len, int val){       int mul_val = 1;       for (int i = 0; i < arr_len; i++)       mul_val = (mul_val * (my_arr[i] % val)) % val;       return mul_val % val;    }    public static void main(String argc[]){       int[] my_arr = new int []{ 35, 100, 69, 99, 27, 88, 12, 25 }; ... Read More

Java Program to find Product of unique prime factors of a number

Alshifa Hasnain
Updated on 23-Dec-2024 18:10:06

641 Views

In this article, we will learn to calculate the product of the unique prime factors of a given number using Java. Prime factorization plays a crucial role in many mathematical and computational problems, and understanding how to work with prime factors can help you solve complex challenges efficiently.  Problem Statement Given a positive integer n, the task is to find the product of the unique prime factors of n. The objective is to calculate the product of all distinct prime factors of n that divide it exactly. Input  68 Output  34 Since the unique prime factors of 68 are 2 ... Read More

Java program to find minimum sum of factors of a number

AmitDiwan
Updated on 09-Dec-2024 21:56:39

454 Views

In mathematics, the factors of a number are the integers that divide the number without leaving a remainder. For a given number, finding the minimum sum of its factors involves identifying a pair of factors whose sum is the smallest among all possible factor pairs. In this article, we will learn to find the minimum sum of factors of a number we will be using the Integer class and Math class in Java − Integer class The Java Integer class serves as a wrapper for the primitive int type, encapsulating a single integer value within an object. For Integer.MAX_VALUE, which provides ... Read More

Java program to expand a String if range is given?

AmitDiwan
Updated on 08-Jul-2020 11:53:10

335 Views

To expand a String if range is given, the Java code is as follows −Example Live Demopublic class Demo {    public static void expand_range(String word) {       StringBuilder my_sb = new StringBuilder();       String[] str_arr = word.split(", ");       for (int i = 0; i < str_arr.length; i++){          String[] split_str = str_arr[i].split("-");          if (split_str.length == 2){             int low = Integer.parseInt(split_str[0]);             int high = Integer.parseInt(split_str[split_str.length - 1]);             while (low

Java Program to Count trailing zeroes in factorial of a number

AmitDiwan
Updated on 08-Jul-2020 11:47:03

826 Views

To count trailing zeroes in factorial of a number, the Java code is as follows −Example Live Demoimport java.io.*; public class Demo{    static int trailing_zero(int num){       int count = 0;       for (int i = 5; num / i >= 1; i *= 5){          count += num / i;       }       return count;    }    public static void main (String[] args){       int num = 1000000;       System.out.println("The number of trailing zeroes in " + num +" factorial is " + ... Read More

Java program to count the occurrence of each character in a string using Hashmap

AmitDiwan
Updated on 21-Jun-2024 12:41:28

12K+ Views

To count the occurrence of each character in a string using Hashmap, the Java code is as follows −Example Live Demoimport java.io.*; import java.util.*; public class Demo{    static void count_characters(String input_str){       HashMap my_map = new HashMap();       char[] str_array = input_str.toCharArray();       for (char c : str_array){          if (my_map.containsKey(c)){             my_map.put(c, my_map.get(c) + 1);          }else{             my_map.put(c, 1);          }       }       for (Map.Entry entry : my_map.entrySet()){ ... Read More

Java program to count the characters in each word in a given sentence

AmitDiwan
Updated on 08-Jul-2020 11:43:04

505 Views

To count the characters in each word in a given sentence, the Java code is as follows −Example Live Demoimport java.util.*; public class Demo{    static final int max_chars = 256;    static void char_occurence(String my_str){       int count[] = new int[max_chars];       int str_len = my_str.length();       for (int i = 0; i < str_len; i++)       count[my_str.charAt(i)]++;       char ch[] = new char[my_str.length()];       for (int i = 0; i < str_len; i++){          ch[i] = my_str.charAt(i);          int find = 0;          for (int j = 0; j

Java program to count set bits in an integer

Shriansh Kumar
Updated on 11-Sep-2024 11:15:02

3K+ Views

In this article, we are given an integer value and the task is to count the total number of set bits of the given integer. For this task, we need to convert the given value into its corresponding binary representation. The binary number of an integer value is represented as the combination of 0's and 1's. Here, the digit 1 is known as the Set bit in the terms of the computer. Problem Statement Write a program in Java to count set bits in an integer − Input  int num = 10 Output binary representation = 1010 set bit ... Read More

Advertisements