In Java, to read input from standard input(via keyboard), Java provides a built-in Scanner Class. In this article, we will understand how to read a number from standard input(via keyboard) in Java. The Scanner.nextInt() method of the Scanner class belongs to java.util package is used to read the number. The java.util.Scanner.nextInt() method scans the next token of the input as an int. The nextInt() method reads numbers in base-10 (decimal numbers) by default. To read numbers in other bases, like binary or hexadecimal, you can use nextInt(radix). Problem Statement Write a Java program to read a number from standard input. ... Read More
In this article, we will understand how to print the left triangle star pattern in Java. This pattern is also known as the Mirrored Right Triangle Star pattern. Below is a demonstration of the same- Input: Number of rows: 6 Output: * * * * * * * * * * * * * * * * * * * * * Printing Left Triangle Star Pattern Following are the ... Read More
In this article, we will learn how to find the sum of N numbers using recursion in Java. Recursion is when a method calls itself repeatedly until a base condition is met. In Java, each recursive call is placed on the stack until the base case is reached, after which values are returned to calculate the result. To solve with recursion, we need to divide our problem into smaller chunks, then find out solution for that smaller part, and that will be our base condition. In this problem, to find the sum of N numbers using recursion, we can store ... Read More
In this article, we will understand how to find the product of two numbers using recursion in Java. A recursive function is a function that calls itself multiple times until a particular condition is satisfied. Product of two Numbers Using Recursion Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function. To find the product of two numbers, let's say a and b, using recursion, the base case will be (if y = 0, ... Read More
In this article, we will understand how to find the sum of the digits of a number using recursion in Java. A recursive function is a function that calls itself multiple times until a particular condition or base condition is met. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function. Problem Statement Write a program in Java to find the sum of the digits of a number using recursion. Below is a demonstration of the same. Input Enter the number : 12131415 ... Read More
To calculate the power of a number, Java provides different ways to do this using loops, built-in methods, and recursion. In this article, we will learn how to calculate the power of a number using recursion. In Java, Recursion is a process in which a function calls itself multiple times until it reaches a base condition, and the corresponding function is called a recursive function. Problem Statement Given a base number and a power value, we need to calculate the result of raising the base to the power using recursion. Below is a demonstration of the same. Input: Enter ... Read More
In this article, we will understand how to find the factorial of a number using recursion in Java. The factorial of a positive number is the product of the number itself and all descending positive integers up to "n". The factorial of a negative number does not exist, and the factorial of 0 is 1. The factorial of a positive number is - factorial of n (n!) = 1 * 2 * 3 * 4 * ... * n Factorial Using a Recursive Function A recursive function is a function that calls itself multiple times until a particular condition is satisfied. ... Read More
In this article, we will understand how to get input from the user in Java. Java provides a built-in Scanner Class that belongs to the java.util package. The Scanner class is used to get user input. In this article, we will learn how to take different types of input, like integers, floating numbers, strings, etc., from the user in Java. Algorithm Following are the steps to get input from the user - Step 1- START Step-2- Import the Scanner class Step 3- Create a Scanner object Step 4- Prompt the user to enter ... Read More
Java HashMap is a hash table-based implementation of Java's Map interface. It is a collection of key-value pairs. In this article, we will understand how to get a key from a HashMap using the value. Following are the ways to get a key from a HashMap using the value: Using For Loop Using Stream API Using For Loop We can iterate through the entries of the HashMap using a for loop and check if the value matches the one we are looking for. If it does, we return the corresponding ... Read More
In this article, we will understand how to check if a Set is a subset of another set. A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are not allowed. Following are the ways to check if a set is a subset of another set: Using containsAll() Method Using Stream API Using containsAll() Method In Java, the set interface has a method called containsAll() that checks ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP