Found 25 Articles for Coding Practice

Java Program to Add Two Complex numbers

Shriansh Kumar
Updated on 01-Aug-2024 10:54:52

2K+ Views

Complex numbers are expressed in the form of p + iq where p and q indicates real numbers and i represents an imaginary number. For instance, 8.2 + 5.6i is a complex number, where 8.2 is the real part and 5.6i is the imaginary part. In this article, we will understand how to add two complex numbers in Java. Example Scenario Input: complex_num = 15 +i24 and 3 +i7 Output: sum = 18 + i31 Program to Add Complex Numbers in Java In this example, we are calculating addition of two complex numbers in Java. import java.util.*; class ComplexNumbers ... Read More

Java program to count set bits in an integer

Shriansh Kumar
Updated on 11-Sep-2024 11:15:02

2K+ Views

In this article, we are given an integer value and the task is to count the total number of set bits of the given integer. For this task, we need to convert the given value into its corresponding binary representation. The binary number of an integer value is represented as the combination of 0's and 1's. Here, the digit 1 is known as the Set bit in the terms of the computer. Problem Statement Write a program in Java to count set bits in an integer − Input  int num = 10 Output binary representation = 1010 set bit ... Read More

JavaScript Program to Check if a Given Year is Leap Year

Shriansh Kumar
Updated on 17-Jan-2025 10:27:10

2K+ Views

To check if a given year is leap year, We have used two approaches in this article. A leap year has 366 days. In every 4th year, an additional day is added to the month of February to synchronize it with astronomical year. In this article, we are given with two years to check. Our task is to write a JavaScript program to check if a given year is leap year. Approaches to Check Leap Year Here is a list of approaches to check if a given year is leap year in Javascript which we will be discussing ... Read More

Java Program to add year to current date using Calendar.add method

Shriansh Kumar
Updated on 16-Aug-2024 08:05:46

1K+ Views

The Calendar class of java.util package provides a method with the name add(). This method accepts current date and amount of time as parameter values. If the given amount of time is positive, it will add it to the current date, and in the case of negative time, it will subtract it. In this article, we will see a few Java programs to add year to the current date using the Calendar.add() method. Example Scenario: Input 1: current_date = Thu Nov 22 18:19:06 UTC 2018 Input 2: year_to_add = 12 Output: new_date = Thu Nov 22 18:19:06 UTC 2030 ... Read More

Java Program to convert Double to numeric primitive data types

Shriansh Kumar
Updated on 30-Jul-2024 16:48:08

2K+ Views

Let's say, we have a numeric value of type double and our task is to convert it into other numeric primitive data types. To perform this operation, we require explicit casting as double is the largest primitive datatype available in Java. The process of converting larger data type into smaller one is called as explicit casting, also known as narrowing casting. Data types are used to specify the type of values stored in different variables. In Java, there are 8 primitive data types, out of which 6 are numeric types, including double, float, int, long, byte, and short. Example Scenario ... Read More

Advertisements