In this article, we will understand how to check if an array contains a given value using two methods: Linear search and the HashSet class. For Linear Search, we will iterate through the array elements, comparing each element with the given input. For the HashSet class, we will utilize the contains() method to achieve this efficiently. Problem Statement Write a Java program to check if an array contains the given value − Input 1 Enter the number to be searched: 25 The elements in the integer array: 15 20 25 30 35 Output 1 The array contains the given value ... Read More
In this article, we will understand how to sum the elements present inside a vector in C++. A vector is a dynamically allocated array with variable size. The sum of elements of a vector can be calculated in a number of ways, and we will discuss two such ways. Problem Statement Given a non-empty vector of integers, calculate the sum of all elements of the vector using multiple approaches in C++. Examples The following examples give the input and output of some test cases : Input vector vec={1, 3, 4, 5, 2, 3} Output ... Read More
Suppose you are given a String named "str". Now, your task is to write a Java program to convert the given string to Byte. String is a class in Java which stores sequence of characters within double quotes and Byte is a wrapper class of java.lang package which wraps value of byte datatype. Example Scenario: Input: String str = "65"; Output: res = 65 Using Byte.valueOf() Method The valueOf() method of Java Byte class is used to convert given string into its corresponding Byte object. It accepts a single numeric string as a parameter value and returns it as ... Read More
For a given input number, write a Java program to check if it is a Fibonacci number. Any number that belongs to Fibonacci series is called as Fibonacci number. The Fibonacci series is a sequence of numbers formed by the sum of its two previous integers. The first two terms of this series are 0 and 1 and further terms go like 1, 2, 3, 5, 8 and so on. This series was named after a famous Italian mathematician, Leonardo Fibonacci. Example Scenario 1 Input: num1 = 8; Output: 8 is a Fibonacci number 8 comes into ... Read More
Given a number, let's say N, write a Java program to check whether the given number is prime or not. Prime numbers are special numbers with only two factors 1 and that number itself, they cannot be divided by any other number. Example Scenario 1 Input: num = 1; Output: 1 is not a prime number Example Scenario 2 Input: num2 = 5; Output: 5 is a prime number 5 is divisible by 1 and 5 only Checking Prime Numbers using Factorization The naive approach to check a given number is prime or not is factorization. ... Read More
In this article, we will understand how to check whether the given input number is a neon number. A neon number is such a number whose sum of digits of square of the number is equal to the number itself. Example Scenario: Input: num = 9; Output: The given input is a neon number How to Check Neon Number in Java? To check a given input is a neon number in Java, use the below approaches − Using for loop Using while loop Using recursion Using ... Read More
In Java, there are a few scenarios where we are required to work with the date and time such as while developing a calendar application, attendance management system in Java and checking age of two persons. Also, the date is a way of keeping track of our time as it is an integral part of our daily lives. Therefore, Java provides classes like Date and LocalDate to work with date and time. In this article, we will discuss how to compare and check if a date is after another or not. Example Scenario 1 Input: dateOne = "2023-01-20"; ... Read More
In Java, both for loop and the for-each loop are used to iterate over each element of a stream or collection like arrays and ArrayList in order to perform desired operations. In this article, we will learn how to iterate over elements of an array using for and for-each loop. Here, the array is a data structure which stores a fixed-size sequential collection of elements of the same data type. Example Scenario 1 Input: String[] designations={"Ravi", "Riya", "Ashish"}; Output: Ravi, Riya, Ashish Example Scenario 2 Input: int[] designations = {2, 4, 5, 7}; Output: {2, 4, 5, 7} ... Read More
In Java, string concatenation is the operation of joining two or more strings together. However, string concatenation can be performed with various primitive data types, not just with other strings. Two strings can be concatenated using concat() method of String class, but, to concatenate strings with other primitive data type you need to use the ‘+’ operator. The given data type will automatically converted to its string representation. Example Scenario: Input: String res = "Age: " + 45; Output: result = Age: 45 String Concatenation with int The int is a primitive datatype in Java which represents numeric data ... Read More
In Java, stream was introduced in Java 8. It represents a collection of elements and supports functional operations on those collections. Here, we are talking about collections like Array, List and Set. The Stream API simply channelizes the elements of sources through various built-in methods to return the desired result. In this article, we are going to discuss different operations that can be performed on the Java stream. Operations on Java 8 Streams There are two types of operations, we can perform on the Java streams − Intermediate Operations − They process the elements of an input stream ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP