Server Side Programming Articles - Page 1718 of 2650

Using predefined class name as Class or Variable name in Java

AmitDiwan
Updated on 09-Jul-2020 06:43:55

1K+ Views

Using a predefined class name as a class nameLet us see an example −Example Live Demopublic 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 −Example Live Demopublic class String{    public static void main (java.lang.String[] args){       System.out.println("Pre-defined class name ... Read More

Using TreeMap to sort User-defined Objects in Java

AmitDiwan
Updated on 09-Jul-2020 06:42:38

213 Views

To sort user-defined object in Java, the code is as follows −Example Live Demoimport 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

Unreachable Code Error in Java

AmitDiwan
Updated on 09-Jul-2020 06:40:33

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 −Example Live Demopublic 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. ");          }       }    } ... Read More

Java program to find the first non-repeating character from a stream of characters

AmitDiwan
Updated on 23-Jul-2024 18:21:33

1K+ Views

Finding the first non-repeating character in a string is a common programming problem. It involves finding the first character that appears only once in the string. This task helps understand how to manipulate strings and use basic data structures in Java. Problem Statement Given a string, identify the first character that does not repeat. If all characters repeat, indicate that there is no non-repeating character. Input tutorialspoint Output The first non-repeating character of the string is T Steps to find the first non-repeating character from a stream of characters Below are the steps to find the first non-repeating character from a ... Read More

Java program to find the number occurring odd number of times

AmitDiwan
Updated on 07-Nov-2024 01:07:17

613 Views

In this article, we will learn how to find the number in an array that appears an odd number of times using Java. By looping through the array and counting occurrences of each number, the program will detect and return the one with an odd frequency. Problem Statement Given an array identify the integer that occurs an odd number of times. Below is the demostration of the same − Input 34, 56, 99, 34, 55, 99, 90, 11, 12, 11, 11, 34 Output The number that occurs odd number of times in the array is 34 Steps to find the number occurring ... Read More

Java Program to Find the closest pair from two sorted arrays

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

350 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

952 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

241 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

669 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

463 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

Advertisements