Swap Case of a String in Java

Samual Sam
Updated on 18-Jun-2024 18:01:46

14K+ Views

To swap the case of a string, use.toLowerCase() for title case string. toLowerCase() for uppercase stringtoUpperCase() for lowercase stringLoop through what we discussed above for all the characters in the sting.for (int i = 0; i > len; i++) {    c = str.charAt(i);    // title case converted to lower case    if (Character.isTitleCase(c)) {       c = Character.toLowerCase(c);    }    // upper case converted to lower case    if (Character.isUpperCase(c)) {       c = Character.toLowerCase(c);    }    // lower case converted to upper case    if (Character.isLowerCase(c)) {       c ... Read More

Compute the Sum of Diagonals of a Matrix in Java

AmitDiwan
Updated on 18-Jun-2024 17:51:56

12K+ Views

In this article, we will understand how to compute the sum of diagonals of a matrix. The matrix has a row and column arrangement of its elements. The principal diagonal is a diagonal in a square matrix that goes from the upper left corner to the lower right corner.The secondary diagonal is a diagonal of a square matrix that goes from the lower left corner to the upper right corner.Below is a demonstration of the same −Suppose our input is −The input matrix: 4 5 6 7 1 7 3 4 11 12 13 14 23 24 25 50The desired ... Read More

JavaScript TypeScript Object Null Check

Nikhilesh Aleti
Updated on 18-Jun-2024 17:36:07

14K+ Views

In this article we will check if an object is a null in Typescript. A variable is undefined until and unless it is not assigned to any value after declaring it. NULL is known as empty or dosen’t exist. In typescript, unassigned values are by default undefined, so in order to make a variable null, we must assign it a null value. To check a variable is null or not in typescript we can use typeof or "===" operator. Using typeofoperator The typeof operator in JavaScript is used to find the datatype of the variable. Example In the example ... Read More

Retrieve All Keys and Values in HashMap in Java

karthikeya Boyini
Updated on 18-Jun-2024 17:04:46

16K+ Views

To retrieve the set of keys from HashMap, use the keyset() method. However, for set of values, use the values() method.Create a HashMap −HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600)); hm.put("Backpack", new Integer(1200));Now, retrieve the keys −Set keys = hm.keySet(); Iterator i = keys.iterator(); while (i.hasNext()) { System.out.println(i.next()); }Retrieve the values −Collection getValues = hm.values(); i = getValues.iterator(); while (i.hasNext()) { System.out.println(i.next()); }The following is an example to get the set of all keys and values in HashMap −Example Live Demoimport java.util.*; public class Demo { public static ... Read More

Find the Area of a Rectangle in Java

Samual Sam
Updated on 18-Jun-2024 16:59:45

15K+ Views

Area of a rectangle is the product of its length and breadth. Therefore, to calculate the area of a rectangleGet the length of the rectangle form the user.Get the breadth of the rectangle form the user.Calculate their product.Print the product.ExampleBelow is an example to find the area of a rectangle in Java using Scanner class.import java.util.Scanner; public class AreaOfRectangle {    public static void main(String args[]){       int length, breadth, area;       Scanner sc = new Scanner(System.in);       System.out.println("Enter the length of the rectangle ::");       length = sc.nextInt();       ... Read More

Fill an Array of Characters from User Input in Java

Samual Sam
Updated on 18-Jun-2024 16:45:18

18K+ Views

For user input, use the Scanner class with System.in. After getting the input, convert it to character array −char[] a = s.next().toCharArray();Now, display it until the length of the character array i.e. number of elements input by the user −for (int i = 0; i < a.length; i++) {    System.out.println(a[i]); }To fill an array of characters from user input, use Scanner class.Exampleimport java.util.Scanner; public class Demo {    public static void main(String args[]) {       Scanner s = new Scanner(System.in);       System.out.println("First add some characters...");       char[] a = s.next().toCharArray();       ... Read More

Find Cube of a Given Number in Java

Samual Sam
Updated on 18-Jun-2024 15:48:41

18K+ Views

Cube of a value is simply three times multiplication of the value with self.For example, cube of 2 is (2*2*2) = 8.AlgorithmSteps to find a cube of a given number in Java programming:Take integer variable A.Multiply A three times.Display result as Cube.Exampleimport java.util.Scanner; public class FindingCube {    public static void main(String args[]){       int n = 5;       System.out.println("Enter a number ::");       Scanner sc = new Scanner(System.in);       int num = sc.nextInt();       System.out.println("Cube of the given number is "+(num*num*num));    } }OutputEnter a number :: 5 Cube of the given number is 125

Java Program for Multiplication of Array Elements

Venkata Sai
Updated on 18-Jun-2024 15:41:39

20K+ Views

To find the product of elements of an array.create an empty variable. (product)Initialize it with 1.In a loop traverse through each element (or get each element from user) multiply each element to product.Print the product.Example Live Demoimport java.util.Arrays; import java.util.Scanner; public class ProductOfArrayOfElements {    public static void main(String args[]){       System.out.println("Enter the required size of the array :: ");       Scanner s = new Scanner(System.in);       int size = s.nextInt();       int myArray[] = new int [size];       int product = 1;       System.out.println("Enter the elements of the ... Read More

Print Armstrong Numbers Between Two Numbers in Java

Ankith Reddy
Updated on 18-Jun-2024 15:34:13

19K+ Views

An Armstrong number is a number which equals to the sum of the cubes of its individual digits. For example, 153 is an Armstrong number as −153 = (1)3 + (5)3 + (3)3 153 1 + 125 + 27 154 153Algorithm1. Take integer variable Arms. 2. Assign a value to the variable. 3. Split all digits of Arms. 4. Find cube-value of each digit. 5. Add all cube-values together. 6. Save the output to Sum variable. 7. If Sum equals to Arms print Armstrong Number. 8. If Sum does not equal to Arms print Not Armstrong Number.Example Below is an ... Read More

Convert Decimal to Binary in Java

Samual Sam
Updated on 18-Jun-2024 15:20:46

24K+ Views

To convert decimal to binary, Java has a method Integer.toBinaryString(). The method returns a string representation of the integer argument as an unsigned integer in base 2.Let us first declare and initialize an integer variable.int dec = 25;Convert it to binary.String bin = Integer.toBinaryString(dec);Now display the “bin” string, which consists of the Binary value. Here is the complete example.Example Live Demopublic class Demo {    public static void main( String args[] ) {       int dec = 25;       // converting to binary and representing it in a string       String bin = Integer.toBinaryString(dec);       System.out.println(bin);    } }Output11001

Advertisements