Found 206 Articles for Programming Languages

Java Program to Find Two Elements Whose Sum is Closest to Zero

Ashin Vincent
Updated on 27-Feb-2025 13:28:34

55 Views

In this article we will learn how to find the two elements in an array whose sum is closest to zero. The array can contain both positive and negative values as well. Let's understand with the help of an example. Example Let's take an array with the following elements − arr = {3, -56, -76, -32, 45, 77, -14, 13, 92, 37} Here, two elements whose sum is closest to zero are 77 and -76, and their sum is 1. Note: In this example we have 77 and -76, which will give the sum as 1, and also ... Read More

Find Square Root of a Number Without the sqrt Method in Java

Ashin Vincent
Updated on 27-Feb-2025 13:15:02

76 Views

In Java, we can easily find the square root of a number using the inbuilt function 'Math.sqrt()’. But sometimes during an interview, the interviewer may ask to write the code from scratch without using inbuilt functions. So in this article, we will discuss the different ways to find the square root of a number without using the sqrt method. 1.Brute Force Method In this brute force method of calculating the square root of a number, first we check if the input is valid or not, and if the input is 0 or 1, we directly return the same number as ... Read More

Length of the longest consecutive zeroes in the binary representation of a number in java

Ashin Vincent
Updated on 27-Feb-2025 12:55:23

54 Views

You are given a number (N); you need to find the length of the longest consecutive zeroes in the binary representation of the number using different approaches. Example Input N = 529 The binary form of 529 is 1000010001. Output The longest chain of zeroes in the binary form is 0000 with a length of 4. Hence, the answer is 4. Approach 1: Using Division and Modulus In this approach we use division and modulus operations to convert decimal to binary and then count the maximum zeroes in the binary number. Steps for implementation The following steps explain ... Read More

Lead Number in java

Ashin Vincent
Updated on 27-Feb-2025 12:43:07

122 Views

A lead number is a number whose sum of even digits is equal to the sum of odd digits. In this article, we will learn about lead numbers and programs to check if a number is a lead number or not. Lead number example Let’s take the number 615341. The sum of even digits is 6 + 4 = 10. The sum of odd digits is 1 + 5 + 3 + 1 = 10. Here, the sum of even digits = the sum of odd digits, so 615341 is a lead number Arithmetic Approach This approach extracts digits ... Read More

Java Program to Implement Associative Array

Ashin Vincent
Updated on 27-Feb-2025 12:28:06

50 Views

What is an associative array? An associative array stores data in key-value pairs where we can retrieve values using unique keys. There is a significant difference between standard arrays and associative arrays. Normal arrays use numbers for indexing (like arr[0], arr[1], arr[2]). Whereas in an associative array, we use meaningful names such as "name" or "city” to identify each value. Example Let's consider a list of student grades where names act as keys − Key (Student Name): Value (Marks) Raju: 85 Krishna: 90 Yash: 78 This lets us find the grades of students by using their names instead of ... Read More

Java Program to Print Odd Elements at Odd Index

Ashin Vincent
Updated on 27-Feb-2025 12:17:42

43 Views

In this article, we will learn how to find and print the odd numbers that are present at odd index positions in an array. First we will understand the problem with the help of an example, and then we will learn three approaches to implement it in Java. Example Consider the array = [2, 1, 4, 4, 5, 7]. The odd numbers at odd indices are 1 and 7. Explanation The indexing of an array starts from 0. Here the element 1, which is an odd number, is present at index 1, which is an odd index. In the same ... Read More

Java Program to Generate Random Hexadecimal Value

Ashin Vincent
Updated on 27-Feb-2025 12:03:49

35 Views

In this article, we will learn how to generate random hexadecimal number values in Java. Random hexadecimal values are used in many applications, like unique identifiers, cryptographic keys, session tokens, or colour codes. We will discuss three different ways to generate random hexadecimal values in Java − Using Random – A simple, fast method to make random hexadecimal values. Using SecureRandom – Best for secure random hexadecimal numbers. Using BigInteger – Helps produce large random hexadecimal numbers. 1. Using Random In this approach, we use the Random class to produce random numbers from 0 to 15. After ... Read More

James Gosling:The Father of Java

Ashin Vincent
Updated on 27-Feb-2025 11:47:13

113 Views

James Arthur Gosling, also known as the Father of Java, is one of the greatest minds in computer science and programming history. He created the Java programming language in 1994, which became a revolutionary invention that transformed the software development industry. Java lets developers run a single code on any device, which makes it a platform-independent language. Other than java, Gosling has worked on several technical areas such as Artificial intelligence, software engineering and cloud computing. Early life and education James Gosling was born on May 19, 1955, in Calgary, Alberta, Canada. He developed an interest in computers at an ... Read More

Java Program to Increment All Elements of an Array by One

Ashin Vincent
Updated on 27-Feb-2025 11:38:50

32 Views

Arrays are one of the most fundamental data structures in programming, and they are used to store elements of the same data type in contiguous memory locations. If you are not familiar with the array concept, do check out this article on Arrays. In this article, we will learn how to increment every element of an array by one and print the incremented array. For example, If the input array is [3, 7, 10] The output should be [4, 8, 11] We can solve this problem using different approaches − Using for-loop Using java streams Using arrays.setAll() ... Read More

How to find hyperfactorial in Java?

Ashin Vincent
Updated on 27-Feb-2025 11:18:17

29 Views

What is hyperfactorial? The hyperfactorial of a number is the product of numbers from 1 to the given number where each number is raised to the power of itself. For example, the hyperfactorial of 4 will be − = 1^1 * 2^2 * 3^3 * 4^4 = 1*4*27*256 = 27648 So we can say that the formula for finding hyperfactorial is − H(n)=1^1 * 2^2 *3^3 * 4^4 * 5^5 * . . . . . . . . . * n^n In this article, we will learn different approaches to find the hyperfactorial of a number, ... Read More

Advertisements