Display Numbers and Sum of First N Natural Numbers in Java

Shriansh Kumar
Updated on 16-Aug-2024 07:34:54

2K+ Views

Natural numbers are all positive integers or whole numbers that range between 1 to infinity. In this article, we will see how to find the sum of the first N natural numbers in Java, where N is the integer up to which we need to add all the numbers starting from 1. Example Scenario: Input: num = 5 Output: sum = 15 sum = 1 + 2 + 3 + 4 + 5 Using a for loop In this approach, initialize a variable with 0 to store the sum. Then, run a for loop that goes from ... Read More

Find Common Elements in Three Sorted Arrays in Java

Shriansh Kumar
Updated on 16-Aug-2024 07:29:57

2K+ Views

The common elements in three sorted arrays are those elements that occur in all three of them. In this article, we will learn how to find common elements from three sorted arrays in Java. An example of this is given as follows − Example Scenario: Input 1: arr1 = [1, 3, 5, 7, 9] Input 2: arr2 = [2, 3, 6, 7, 9] Input 3: arr3 = [1, 2, 3, 4, 5, 6, 7, 8, 9] Output: Common elements = 3 7 9 Here, arrays are data structure which stores a fixed-size sequential collection of elements of the same ... Read More

Multiply Given Floating Point Numbers in Java

Shriansh Kumar
Updated on 16-Aug-2024 07:29:13

1K+ Views

Suppose two floating point numbers are given as operands and your task is to write a Java program to multiply the given numbers. To perform this operation, initialize two float values, multiply and store the result in another float type variable. Float is a datatype in Java which stores numbers with fractional part. Example Scenario: Input 1: num1 = 1.2 Input 2: num2 = 1.4 Output: product = 1.68 Using Multiplication Operator The multiplication operator is represented by asterisk sign (*). It is categorized under arithmetic operator in Java. It can be used to multiply float values ... Read More

Get Day of Week as String in Java

Shriansh Kumar
Updated on 16-Aug-2024 07:28:17

3K+ Views

Some applications that work on calendars require a day name to be displayed for features like scheduling tasks, events or reminders. For this purpose, Java provides various built-in classes and methods including LocalDate, Calendar and SimpleDateFormat. In this article, we will learn how to use these classes and methods in Java programs to find the day name of a week for a given date. Using LocalDate Class In this approach, we first find current date using LocalDate class and using its built-in method named getDayOfWeek(), we create a DayOfWeek Enum which can be converted to String to display day ... Read More

Find the Perimeter of a Circle in Java

Shriansh Kumar
Updated on 15-Aug-2024 13:05:46

1K+ Views

For a given circle with radius "r", write a Java program to find the perimeter of that circle. The circumference is also known as the perimeter. It's the distance around a circle. Circumference is given by the formula C = 2𝜋r where, pi/𝜋 = 3.14 and r is the radius of the circle − Example Scenario Input: radius = 5 Output: perimeter = 31.400000000000002 2 * 3.14 * 5 = 31.428571428571427 By Defining Constant Value for PI In this Java program, we define the value of PI as a constant and find the perimeter of circle ... Read More

Join Two Given Lists in Java

Arjun Thakur
Updated on 14-Aug-2024 22:21:48

3K+ Views

To join two given lists the addAll() method of the java.util.ArrayList class is used to insert all of the elements in the specified collection into this list. To add contents of a list to another − Problem Statement Write a program in Java to join two given lists − Input Contents of list1 ::[Apple, Orange, Banana] Contents of list2 ::[Grapes, Mango, Strawberry] Output Contents of list1 after adding list2 to it ::[Apple, Orange, Banana, Grapes, Mango, Strawberry] Steps to join two given lists Following are the steps to join two given lists − ... Read More

Subarray with Given Sum in Java - Different Approaches

Areeba Rashid
Updated on 14-Aug-2024 22:07:51

281 Views

Finding a subarray with a given sum is a common problem that often appears in coding interviews and competitive programming. This problem can be solved using various techniques, each with its own trade-offs regarding time complexity and space complexity. In this article, we'll explore multiple approaches to solving the problem of finding a subarray with a given sum in Java. Problem Statement Given an array of integers and a target sum, find a continuous subarray in the array that adds up to the given sum. The problem can be divided into two main variants: Subarray ... Read More

Remove Duplicates from a Given Stack in Java

Anh Tran Tuan
Updated on 14-Aug-2024 18:45:36

527 Views

In this article, we’ll explore two methods to remove duplicate elements from a stack in Java. We’ll compare a straightforward approach with nested loops and a more efficient method using a HashSet. The goal is to demonstrate how to optimize duplicate removal and to evaluate the performance of each approach. Problem statement Write a Java program to remove the duplicate element from the stack. Input Stack data = initData(10L); Output Unique elements using Naive Approach: [1, 4, 3, 2, 8, 7, 5] Time spent for Naive Approach: 18200 nanoseconds Unique elements using Optimized Approach: [1, 4, 3, 2, ... Read More

Wildcard Selectors in CSS for Classes

Shubham Vora
Updated on 14-Aug-2024 17:30:40

14K+ Views

CSS wildcard selectors allow us to select an HTML element containing the particular substring in the value of the particular attribute, such as classes, id or other attributes. It is useful when style is applied to multiple elements having a common pattern in their attributes. In this article, we will be learning about wildcard selectors in CSS and it's types which are mentioned below. Contains (*=) wildcard selector Starts with (^=) wildcard selector Ends with ($=) wildcard selector Contains (*=) wildcard selector CSS contains(*=) wildcard ... Read More

Create the Indian Flag Using HTML and CSS

Harshit Sachan
Updated on 14-Aug-2024 13:45:20

14K+ Views

To create the Indian flag using HTML and CSS, we will need to have good understanding of CSS. Indian flag have three colors: saffron, white and green and Ashoka Chakra at the middle. In this article, we will be creating stepwise Indian flag. First making a pole then tricolor and then rotating Ashoka chakra all with the help of HTML and CSS only. Later we will be animating our flag with wave animation and flag movement. These are the steps which we will be following: .flag-container { ... Read More

Advertisements