In this article, we will learn to find the common divisors of two numbers using Java. The program will use a recursive method to calculate the greatest common divisor (GCD) of the two numbers, and then determine how many divisors are shared by both numbers. The output will display the total number of common divisors. Problem Statement Write a Java program to find and count the common divisors of two given numbers. Below is a demonstration of the same − Input val_1= 68 val_2= 34 Output The common divisors between the two numbers is4 Steps to find the common divisors ... Read More
In this program, we will perform an iterative quick sort in Java to sort an array of integers. Instead of using recursion, the program uses an iterative approach with an auxiliary stack to manage the sorting process. The output will display the sorted array after applying the quick sort algorithm. Problem Statement Write a Java program to sort an array of integers using the iterative quick sort method. Below is the demonstration of the same − Input {34, 76, 41, 32, 11, 0, 91, 102, -11} Output After iteratively performing quick sort, the array is -11 0 11 32 34 ... Read More
The Fibonacci Series generates subsequent numbers by adding two previous numbers. The Fibonacci series starts from two numbers − F0 & F1. The initial values of F0 & F1 can be taken as 0, 1, or 1, 1 respectively. Fn = Fn-1 + Fn-2 Problem Statement Write a program in Java program to print a Fibonacci series. Input Run the program Output 1 1 2 3 5 8 13 21 34 55 The above output is obtained when the values of n=10. Different approaches to print a Fibonacci series Following are the different approaches to printing a Fibonacci series − ... Read More
In this article, we will learn about constructors in Java. Constructors initialize objects when they are created. We will also see how the parent constructor is called in inheritance. Problem StatementWe will demonstrate possible approaches to show how the inherited constructor calls the parent constructor by default.Different approaches The following are the different approaches to show how the inherited constructor calls the parent constructor by default− Demonstrating inheritance properties ... Read More
In this article, we calculate the number of minutes in a given duration using Java. The Duration class can perform time-based calculations like hours, minutes, days, etc.. and easily convert between these units. Using the toMinutes() method you will be able to get the number of minutes from a given Duration. Problem StatementGiven a Duration object representing a period of time, write a Java program to calculate the number of minutes in the specified duration.Input Duration = 25 days, 10 hoursOutput Minutes in 25 days = 36000Minutes in 10 hours = 600 Steps to get the number of ... Read More
In this article, we use the System.nanoTime() method in Java to get the current value of the system timer in nanoseconds. It returns the current value of the most precise available system timer, in nanoseconds. Problem StatementGiven that you need to get the current value of the system timer in nanoseconds, write a Java program that retrieves this value using the System.nanoTime() method.Input There is no specific input as the program retrieves the system's current time in nanoseconds directly.Output Current Value: 49908709882168 nanoseconds Steps to get the current value of the system timer in nanosecondsThe following are the ... Read More
In this article, we will learn how to count the number of elements with odd factors (i.e., perfect squares) in a given range using Java. Perfect squares have odd divisors, and we can calculate them by counting the perfect squares in the specified range. Problem StatementGiven a range defined by a lower and upper bound, write a Java program to calculate how many numbers in this range have odd factors (i.e., perfect squares).Input Lower Range: 55Upper Range: 1000Output The number of elements with odd factors between 55 and 1000 is: 24 Steps to calculate the number of elements with ... Read More
In this article, we will learn how to create a Java program that arranges buttons in three vertical columns with an equal number of buttons using GridLayout. The GridLayout class allows us to arrange components in a grid format, which makes it ideal for creating a uniform layout of buttons. Problem StatementGiven a set of 12 buttons, we need to create a Java Swing application that organizes these buttons into three vertical columns. Each column should contain an equal number of buttons.Input No direct input is required from the user. The program will automatically generate 12 buttons labeled as ... Read More
This example demonstrate about How to dynamically remove items from ListView on a click Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken listview. Step 3 − Add the following code to src/MainActivity.java package com.example.myapplication; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity { TextView textView; ArrayAdapter adapter = null; String[] ... Read More
To create an image element dynamically using JavaScript, we can use various approach based on our use case. We will be understanding two such approach in this article with examples and stepwise explaination of examples. In this article our task is to create an image element dynamically using JavaScript. Approaches to Create an Image Element Here is a list of approaches to create an image element dynamically using JavaScript which we will be discussing in this article with stepwise explaination and complete example codes. Using createElement() Method Using Image() Constructor ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP