Found 2617 Articles for Java

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

AmitDiwan
Updated on 08-Jul-2020 11:34:00

206 Views

To check if all digits of a number divide it, the Java code is as follows −Example Live Demoimport java.io.*; public class Demo{    static boolean divisibility_check(int val, int digit){       return (digit != 0 && val % digit == 0);    }    static boolean divide_digits(int val){       int temp = val;       while (temp > 0){          int digit = val % 10;          if ((divisibility_check(val, digit)) == false)          return false;          temp /= 10;       }   ... Read More

Java Program to calculate Sum of squares of first n natural numbers

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

666 Views

To calculate the sum of squares of first n natural numbers, the Java code is as follows −Example Live Demoimport java.io.*; import java.util.*; public class Demo{    public static int sum_of_squares(int val){       return (val * (val + 1) / 2) * (2 * val + 1) / 3;    }    public static void main(String[] args){       int val = 8;       System.out.println("The sum of squares of first 8 natural numbers is ");       System.out.println(sum_of_squares(val));    } }OutputThe sum of squares of first 8 natural numbers is 204A class named Demo contains ... Read More

Java Program for Smallest K digit number divisible by X

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

159 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 08-Jul-2020 11:16:57

188 Views

To get number of elements with odd factors in given range, the Java code is as follows −Exampleimport java.io.*; import java.util.*; import java.lang.*; public class Demo{    public static int square_count(int low_range, int high_range){       return (int)Math.pow((double)high_range, 0.5) - (int)Math.pow((double)low_range-1, 0.5);    }    public static void main (String[] args){       int low_range = 55, high_range = 1000;       System.out.print("The number of values with odd factors between a given range of numbers       is : " + square_count(low_range, high_range));    } }OutputThe number of values with odd factors between a given range ... Read More

Java Program for nth multiple of a number in Fibonacci Series

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

189 Views

To find the nth multiple of a number in Fibonacci series, the Java code is as follows −Example Live Demopublic class Demo{    public static int position(int pos, int num){       long val_1 = 0, val_2 = 1, val_3 ;       int i = 2;       while(i != 0){          val_3 = val_1 + val_2;          val_1 = val_2;          val_2 = val_3;          if(val_2 % pos == 0){             return num * i;         ... Read More

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

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

291 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

Static and non static blank final variables in Java

AmitDiwan
Updated on 08-Jul-2020 10:28:41

1K+ Views

Static variable: Declared with the help of the keyword ‘static’, they are also known as class variables. They are defined within a constructor or outside a class function. When a variable is static, it is shared between all objects of the class, irrespective of the number of objects that are created.Demonstrating how the ‘static’ keyword, when used with variable works −Example Live Demopublic class Demo{    String name;    static String designation;    public void display_data(){       System.out.println("The name is: " + name);       System.out.println("The designation of this team members is : " + designation);    } ... Read More

Java program to find IP Address of the client

AmitDiwan
Updated on 08-Jul-2020 10:25:22

1K+ Views

To find the IP Address of the client, the Java code is as follows −Example Live Demoimport java.net.*; import java.io.*; import java.util.*; import java.net.InetAddress; public class Demo{    public static void main(String args[]) throws Exception{       InetAddress my_localhost = InetAddress.getLocalHost();       System.out.println("The IP Address of client is : " + (my_localhost.getHostAddress()).trim());       String my_system_address = "";       try{          URL my_url = new URL("http://bot.whatismyipaddress.com");          BufferedReader my_br = new BufferedReader(new          InputStreamReader(my_url.openStream()));          my_system_address = my_br.readLine().trim();       }   ... Read More

Java program to check order of characters in string

AmitDiwan
Updated on 08-Jul-2020 10:23:37

1K+ Views

To check order of characters in string in Java, the code is as follows −Example Live Demopublic class Demo{    static boolean alphabetical_order(String my_str){       int str_len = my_str.length();       for (int i = 1; i < str_len; i++){          if (my_str.charAt(i) < my_str.charAt(i - 1)){             return false;          }       }       return true;    }    static public void main(String[] args{       String my_str = "abcmnqxz";       if (alphabetical_order(my_str)){          System.out.println("The letters ... Read More

Advertisements