Found 2617 Articles for Java

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

AmitDiwan
Updated on 08-Jul-2020 11:56:25

474 Views

To find product of unique prime factors of a number, the Java code is as follows −Example Live Demopublic class Demo {    public static long prime_factors(int num){       long my_prod = 1;       for (int i = 2; i

Java Program to find minimum sum of factors of a number

AmitDiwan
Updated on 08-Jul-2020 11:54:26

290 Views

To find minimum sum of factors of a number, the Java code is as follows −Example Live Demopublic class Demo {    static int minimum_sum(int num){       int my_sum = 0;       for (int i = 2; i * i

Java program to expand a String if range is given?

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

220 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

693 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

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

348 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

AmitDiwan
Updated on 08-Jul-2020 11:40:27

941 Views

To count set bits in an integer, the Java code is as follows −Example Live Demoimport java.io.*; public class Demo{    static int set_bits_count(int num){       int count = 0;       while (num > 0){          num &= (num - 1);          count++;       }       return count;    }    public static void main(String args[]){       int num =11;       System.out.println("The number of set bits in 11 is ");       System.out.println(set_bits_count(num));    } }OutputThe number of set bits in 11 ... Read More

Java Program to Convert Iterator to Spliterator

AmitDiwan
Updated on 08-Jul-2020 11:39:44

94 Views

To convert Iterator to Spliterator, the Java code is as follows −Example Live Demoimport java.util.*; public class Demo{    public static Spliterator getspiliter(Iterator iterator){       return Spliterators.spliteratorUnknownSize(iterator, 0);    }    public static void main(String[] args){       Iterator my_iter = Arrays.asList(56, 78, 99, 32, 100, 234).iterator();       Spliterator my_spliter = getspiliter(my_iter);       System.out.println("The values in the spliterator are : ");       my_spliter.forEachRemaining(System.out::println);    } }OutputThe values in the spliterator are : 56 78 99 32 100 234A class named Demo contains a function named ‘getspiliter’ that returns a spliterator. In ... Read More

Java Program to check whether it is possible to make a divisible by 3 number using all digits in an array

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

151 Views

To check whether it is possible to make a divisible by 3 number using all digits in an array, the Java code is as follows −Example Live Demoimport java.io.*; import java.util.*; public class Demo{    public static boolean division_possible(int my_arr[], int n_val){       int rem = 0;       for (int i = 0; i < n_val; i++)       rem = (rem + my_arr[i]) % 3;       return (rem == 0);    }    public static void main(String[] args){       int my_arr[] = { 66, 90, 87, 33, 123}; ... Read More

Java Program to check if count of divisors is even or odd

AmitDiwan
Updated on 08-Jul-2020 11:35:48

128 Views

To check if the count of divisors is even or odd, the Java code is as follows −Example Live Demoimport java.io.*; import java.math.*; public class Demo{    static void divisor_count(int n_val){       int root_val = (int)(Math.sqrt(n_val));       if (root_val * root_val == n_val){          System.out.println("The number of divisors is an odd number");       }else{          System.out.println("The number of divisors is an even number");       }    }    public static void main(String args[]) throws IOException{       divisor_count(25);    } }OutputThe number of divisors is an ... Read More

Advertisements