AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 622 of 840

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

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 608 Views

To count the characters in each word in a given sentence, the Java code is as follows −Exampleimport 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

Read More

Java Program to Count trailing zeroes in factorial of a number

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 903 Views

To count trailing zeroes in factorial of a number, the Java code is as follows −Exampleimport 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 expand a String if range is given?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 430 Views

To expand a String if range is given, the Java code is as follows −Examplepublic 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

Read More

Java Program to find reminder of array multiplication divided by n

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 288 Views

To find reminder of array multiplication divided by n, the Java code is as follows −Exampleimport 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 the closest pair from two sorted arrays

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 400 Views

To find the closest pair from two sorted array, the Java code is as follows −Examplepublic 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;             diff ...

Read More

Unreachable Code Error in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ Views

Unreachable code error occurs when the code can’t be compiled due to a variety of reasons, some of which include: infinite loop, return statement before the unreachable line of code.Let us see an example −Examplepublic class Demo{    public static void main(String args[]){       int val = 5;       for (;;){          if (val == 5){             break;             System.out.println("If the condition is not true, this line would be printed. ");          }       }    } }Output/Demo.java:11: ...

Read More

Using TreeMap to sort User-defined Objects in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 276 Views

To sort user-defined object in Java, the code is as follows −Exampleimport java.io.*; import java.util.*; public class Demo{    static void sort_objects(String my_data){       String[] my_vals = my_data.split(" ");       Map my_map = new TreeMap();       for (int i = 1; i < my_vals.length; i += 2){          int my_age = Integer.parseInt(my_vals[i]);          String name = my_vals[i - 1];          if (my_map.containsKey(my_age)){             ArrayList my_list = my_map.get(my_age);             my_list.add(name);             ...

Read More

Using predefined class name as Class or Variable name in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ Views

Using a predefined class name as a class nameLet us see an example −Examplepublic class Number{    public static void main (String[] args){       System.out.println("Pre-defined class name can be used as a class name");    } }OutputPre-defined class name can be used as a class nameThe class Number has a main function that displays a message when it is executed. The main function takes string values as arguments.Using a predefined class name as a variable nameLet us see an example −Examplepublic class String{    public static void main (java.lang.String[] args){       System.out.println("Pre-defined class name can be ...

Read More

Using underscore in Numeric Literals in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 252 Views

Followig is the code showing how to use underscore in numeric literals in Java −Examplepublic class Demo{    public static void main (String[] args) throws java.lang.Exception{       int my_num_1 = 6_78_00_120;       System.out.println("The number is : " + my_num_1);       long my_num_2 = 2_00_11_001;       System.out.println("The number is : " + my_num_2);       float my_num_3 = 4.01_981F;       System.out.println("The number is : " + my_num_3);       double my_num_4 = 12.89_46_061;       System.out.println("The number is : " + my_num_4);    } }OutputThe number is : ...

Read More

Ways to read input from console in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 5K+ Views

Let us see some ways to read input from console in Java −Exampleimport java.util.Scanner; public class Demo{    public static void main(String args[]){       Scanner my_scan = new Scanner(System.in);       String my_str = my_scan.nextLine();       System.out.println("The string is "+my_str);       int my_val = my_scan.nextInt();       System.out.println("The integer is "+my_val);       float my_float = my_scan.nextFloat();       System.out.println("The float value is "+my_float);    } }OutputThe string is Joe The integer is 56 The float value is 78.99A class named Demo contains the main function. An instance of the ...

Read More
Showing 6211–6220 of 8,392 articles
« Prev 1 620 621 622 623 624 840 Next »
Advertisements