Found 9150 Articles for Object Oriented Programming

Java Program to flip a bit in a BigInteger

Samual Sam
Updated on 25-Jun-2020 11:02:24

184 Views

To flip a bit in a BigInteger in Java, use the flipBit() method. This method returns a BigInteger whose value is equivalent to this BigInteger with the designated bit flipped.Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one, two;       one = new BigInteger("7");       one = one.flipBit(3);       System.out.println("Result: " +one);    } }OutputResult: 15Let us see another example.Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger bi1, bi2;       bi1 = ... Read More

Clear a bit in a BigInteger in Java

karthikeya Boyini
Updated on 25-Jun-2020 11:06:01

262 Views

To clear a bit in a BigInteger in Java, use the clearBit() method. It returns a BigInteger whose value is equivalent to this BigInteger with the designated bit cleared.Example Live Demoimport java.math.*; public class BigIntegerDemo {    public static void main(String[] args) {       BigInteger one, two;       one = new BigInteger("7");       two = one.clearBit(2);       System.out.println("Result: " +two);    } }OutputResult: 3Let us see another example.Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger bi1, bi2;       bi1 = new ... Read More

Set a bit for BigInteger in Java

Samual Sam
Updated on 25-Jun-2020 11:07:41

210 Views

The setBit() method is used in Java to return a BigInteger whose value is equivalent to this BigInteger with the designated bit set.Example Live Demoimport java.math.*; public class BigIntegerDemo {    public static void main(String[] args) {       BigInteger one, two;       one = new BigInteger("7");       two = one.setBit(3);       System.out.println("Result: " +two);    } }OutputResult: 15Let us see another example.Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger bi1, bi2;       bi1 = new BigInteger("9");       // setbit ... Read More

Java program to check if string is pangram

Shriansh Kumar
Updated on 18-Apr-2025 18:49:33

12K+ Views

The given task is to write a Java program to check whether a string is a pangram or not. A string is called a pangram if and only if it contains all the letters of the English alphabet, regardless of their case. Example Scenario:Let's understand the problem with an example - Input: "The quick brown fox jumps over the lazy dog" Output: Yes, the string is a pangram Read the input String, you can find all the letters of the English alphabet in it. Therefore, it is a pangram string. How to Check if a String is a Pangram ... Read More

Ternary Operator in Java

Samual Sam
Updated on 25-Jun-2020 11:14:54

2K+ Views

A ternary operator uses 3 operands and it can be used to replace the if else statement. This can be done to make the code simpler and more compact.The syntax of the ternary operator is given as follows −Expression ? Statement 1 : Statement 2In the above syntax, the expression is a conditional expression that results in true or false. If the value of the expression is true, then statement 1 is executed otherwise statement 2 is executed.A program that demonstrates the ternary operator in Java is given as follows.Example Live Demopublic class Example {    public static void main(String[] args) ... Read More

Java program to print the diamond shape

Samual Sam
Updated on 23-Nov-2024 03:55:38

328 Views

In this article, we will learn how to print a diamond shape pattern using nested loops in Java. This helps in understanding loops and conditional structures in Java programming.A nested loop refers to a loop placed within another loop. This structure is often called "loops within loops" because the inner loop runs completely each time the outer loop iterates.Diamond Shape A diamond shape is a symmetrical pattern that consists of two parts: an upper triangle and a mirrored inverted triangle below it. In programming, such patterns are typically printed using nested loops to control spaces and symbols. A diamond ... Read More

Basic calculator program using Java

karthikeya Boyini
Updated on 19-Aug-2024 18:30:26

23K+ Views

In this article, we will learn to create a basic calculator using Java. With a basic calculator, we can add, subtract, multiply, or divide two numbers. This is done using a switch case. A program that demonstrates this is given as follows − Problem Statement Write a program in Java to create a basic calculator for performing the basic arithmetic operations − Input Enter two numbers: 23Enter an operator (+, -, *, /): + Output The result is given as follows:2.0 + 3.0 = 5.0 Steps to create basic calculator Following are the steps to create a basic calculator program using ... Read More

Java program to find common elements in three sorted arrays

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

1K+ 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

Java program to count occurrences of a word in string

Samual Sam
Updated on 31-May-2024 16:34:38

14K+ Views

The number of times a word occurs in a string denotes its occurrence count. An example of this is given as follows −String = An apple is red in colour. Word = red The word red occurs 1 time in the above string.A program that demonstrates this is given as follows.Example Live Demopublic class Example { public static void main(String args[]) { String string = "Spring is beautiful but so is winter"; String word = "is"; String temp[] = string.split(" "); int count = 0; for (int i = 0; i < temp.length; i++) { if (word.equals(temp[i])) count++; } System.out.println("The string ... Read More

Java program to extract ‘k’ bits from a given position

karthikeya Boyini
Updated on 25-Jun-2020 10:04:23

541 Views

Extraction of k bits from the given position in a number involves converting the number into its binary representation. An example of this is given as follows −Number = 20 Binary representation = 10100 k = 3 Position = 2 The bits extracted are 010 which represent 2.A program that demonstrates this is given as follows.Example Live Demopublic class Example {    public static void main (String[] args) {       int number = 20, k = 3, pos = 2;       int exNum = ((1 > (pos - 1));       System.out.println("Extract " + k + ... Read More

Advertisements