Java Articles

Page 8 of 450

Java Program to Check if two of three Boolean variables are true

Manisha Chand
Manisha Chand
Updated on 11-Mar-2026 541 Views

In this article, we will learn to check if any two out of three boolean values are true or not. Boolean variables are datatypes that contain only true or false values. Below is a demonstration of the same - Input: true, true, falseOutput : Two of the three variables are true Checking if two of three Boolean variables are trueIn this article, we will discuss two approaches to check it, and they are - Using if-else Condition ...

Read More

Java Program to Generate Multiplication Table

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ Views

In this article, we will understand how to print a multiplication table. Multiplication table is created by iterating the required input 10 times using a for loop and multiplying the input value with numbers from 1 to 10 in each iteration.Below is a demonstration of the same −InputSuppose our input is −Input : 16OutputThe desired output would be −The multiplication table of 16 is : 16 * 1 = 16 16 * 2 = 32 16 * 3 = 48 16 * 4 = 64 16 * 5 = 80 16 * 6 = 96 16 * 7 = 112 ...

Read More

Java Program to Round a Number to n Decimal Places

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 425 Views

In this article, we will understand how to round a number to n decimal places. Rounding of decimal values are done using the CEIL or FLOOR functions.Below is a demonstration of the same −InputSuppose our input is −Input : 3.1415OutputThe desired output would be −Output : 3.2 AlgorithmStep 1 - START Step 2 - Declare a float variable values namely my_input. Step 3 - Read the required values from the user/ define the values Step 4 – Use the CEIL function to round the number to the required decimal places. In this example we are rounding up to 2 decimal ...

Read More

Java Program to pass method call as arguments to another method

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 22K+ Views

In this article, we will understand how to pass method call as arguments to another method. We can call a method from another class by just creating an object of that class inside another class. After creating an object, call methods using the object reference variable.Below is a demonstration of the same −InputSuppose our input is −Enter two numbers : 2 and 3OutputThe desired output would be −The cube of the sum of two numbers is: 125AlgorithmStep 1 - START Step 2 - Declare two variables values namely my_input_1 and my_input_2 Step 3 - We define a function that takes ...

Read More

Java Program to Print Spiral Pattern of Numbers

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ Views

In this article, we will understand how to print spiral pattern of numbers. The pattern is formed by using multiple for-loops and print statements.Below is a demonstration of the same −InputSuppose our input is −Enter the size : 5OutputThe desired output would be −The spiral pattern 5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4 5 5 4 3 3 3 3 3 4 5 5 3 2 2 4 2 3 4 5 5 4 3 2 1 2 3 4 5 5 4 3 2 2 2 3 4 5 ...

Read More

Java Program to Print Inverted Star Pattern

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ Views

In this article, we will understand how to print inverted star pattern. The pattern is formed by using multiple for-loops and print statements.Below is a demonstration of the same −InputSuppose our input is −Enter the size : 8OutputThe desired output would be −The inverted star pattern *************** ************* *********** ********* ******* ***** *** *AlgorithmStep 1 - START Step 2 - Declare four integer values namely i, j, k and my_input. Step 3 ...

Read More

How to use labels in Java code?

usharani
usharani
Updated on 11-Mar-2026 11K+ Views

Java provides two types of branching/control statements namely, break and continue.The break statementThis statement terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.ExampleFollowing is the example of the break statement. Here we are trying to print elements up to 10 and, using break statement we are terminating the loop when the value in the loop reaches 8.public class BreakExample {    public static void main(String args[]){       for(int i=0; i

Read More

How to check if array contains three consecutive dates in java?

Nikitha N
Nikitha N
Updated on 11-Mar-2026 1K+ Views

To check to find whether a given array contains three consecutive dates:Convert the given array into a list of type LocalDate.Using the methods of the LocalDate class compare ith, i+1th and i+1th, i+2th elements of the list if equal the list contain 3 consecutive elements.Exampleimport java.time.LocalDate; import java.time.Month; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; public class ConsicutiveDate {    public static void main(String args[]) {       String[] dates = {"5/12/2017", "6/12/2017", "7/12/2017"};       List localDateList = new ArrayList();       for (int i = 0; i

Read More

How to move an element of an array to a specific position (swap)?

Sharon Christine
Sharon Christine
Updated on 11-Mar-2026 3K+ Views

To move an element from one position to other (swap) you need to –Create a temp variable and assign the value of the original position to it.Now, assign the value in the new position to original position.Finally, assign the value in the temp to the new position.Exampleimport java.util.Arrays; public class ChangingPositions {    public static void main(String args[]) {       int originalPosition = 1;       int newPosition = 1;       int [] myArray = {23, 93, 56, 92, 39};       int temp = myArray[originalPosition];             myArray[originalPosition] = myArray[newPosition];       myArray[newPosition] = temp;       System.out.println(Arrays.toString(myArray));    } }Output[23, 39, 56, 92, 93]

Read More

How to Convert a Java 8 Stream to an Array?

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 508 Views

To convert a stream to an Array in Java -Collect the stream to a list using the Collect interface and the Collectors class.Now convert the list to an array using the toArray() method.Exampleimport java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class J8StreamToArray {    public static void main(String args[]) {       String[] myArray = { "JavaFX", "OpenCV", "WebGL", "HBase" };       Stream stream = Stream.of(myArray);       List list = stream.collect(Collectors.toList());       String[] str = list.toArray(new String[0]);       System.out.println(Arrays.toString(str));    } }Output[JavaFX, OpenCV, WebGL, HBase]

Read More
Showing 71–80 of 4,496 articles
« Prev 1 6 7 8 9 10 450 Next »
Advertisements