Found 7442 Articles for Java

Java Program to Convert Iterator to Spliterator

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

137 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

232 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

211 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

Java program to check if all digits of a number divide it

AmitDiwan
Updated on 03-Dec-2024 22:22:56

303 Views

The given article involves determining if all digits of a positive integer can divide the number without leaving a remainder. If any digit is zero or does not divide the number the result is false; otherwise, it is true using Java. This can be solved using two approaches: the Naive-Based Approach, which relies on arithmetic operations, and the String-Based Approach, which uses string manipulation for digit processing. Both methods ensure each digit meets the divisibility condition efficiently. Approaches to Check If All Digits of a Number Divide It Following are the different approaches to check if all digits of a ... Read More

Java program to calculate sum of squares of first n natural numbers

AmitDiwan
Updated on 11-Sep-2024 12:25:33

944 Views

In this article, you will learn to write a program in Java to calculate the sum of squares of first n natural numbers. This program efficiently computes the sum using a mathematical formula − (val * (val + 1) / 2) * (2 * val + 1) / 3 Problem Statement Write a program in Java to calculate the sum of squares of first n natural numbers, where n is a user-defined value. Input val = 8 Output The sum of squares of first 8 natural numbers is204 Steps to calculate sum of squares of first n natural numbers Following are ... Read More

Java Program for Smallest K digit number divisible by X

AmitDiwan
Updated on 08-Jul-2020 11:23:11

213 Views

To find the smallest K digit number divisible by X, the Java code is as follows −Example Live Demoimport java.io.*; import java.lang.*; public class Demo{    public static double smallest_k(double x_val, double k_val){       double val = 10;       double MIN = Math.pow(val, k_val - 1);       if (MIN % x_val == 0)       return (MIN);       else       return ((MIN + x_val) - ((MIN + x_val) % x_val));    }    public static void main(String[] args){       double x_val = 76;       double k_val ... Read More

Java program to get number of elements with odd factors in given range

AmitDiwan
Updated on 18-Nov-2024 22:29:39

289 Views

In this article, we will learn how to count the number of elements with odd factors (i.e., perfect squares) in a given range using Java. Perfect squares have odd divisors, and we can calculate them by counting the perfect squares in the specified range. Problem StatementGiven a range defined by a lower and upper bound, write a Java program to calculate how many numbers in this range have odd factors (i.e., perfect squares).Input Lower Range: 55Upper Range: 1000Output The number of elements with odd factors between 55 and 1000 is: 24 Steps to calculate the number of elements with ... Read More

Java program for nth multiple of a number in Fibonacci Series

AmitDiwan
Updated on 05-Nov-2024 22:02:55

324 Views

In this article, we will learn how to find the nth multiple of a given number in the Fibonacci series using Java. The Fibonacci series is a sequence where each number is the sum of the two preceding ones. We will use loops, conditional statements, and the modulus operator to find the position of the nth multiple of a specific number in the series.  Problem StatementGiven a number and its multiple positions, find the position of the nth multiple of a number in the Fibonacci series. Input A number whose multiple is to be found: 9The nth ... Read More

Replacing ‘public’ with ‘private’ in “main” in Java

AmitDiwan
Updated on 08-Jul-2020 10:33:09

395 Views

When ‘public’ is used in ‘main’ −Example Live Demopublic class Demo{    public static void main(String args[]){       System.out.println("This is a sample only");    } }OutputThis is a sample onlyA class named Demo contains the main function that is public. It has a print function, which successfully compiles, executes and prints the message on the console.When ‘public’ is replaced with ‘private’Example Live Demopublic class Demo{    private static void main(String args[]){       System.out.println("This is a sample only");    } }OutputError: Main method not found in class Demo, please define the main method as: public static void main(String[] args) ... Read More

Replace null values with default value in Java Map

AmitDiwan
Updated on 08-Jul-2020 10:29:55

1K+ Views

To replace null values with default value in Java Map, the code is as follows −Example Live Demoimport java.util.*; import java.util.stream.*; public class Demo{    public static Map null_vals(Map my_map, T def_val){       my_map = my_map.entrySet().stream().map(entry -> {          if (entry.getValue() == null)          entry.setValue(def_val);          return entry;       })       .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));       return my_map;    }    public static void main(String[] args){       Map my_map = new HashMap();       my_map.put(1, null);       my_map.put(2, 56);   ... Read More

Advertisements