karthikeya Boyini

karthikeya Boyini

1,421 Articles Published

Articles by karthikeya Boyini

Page 14 of 143

How to use headSet() method of Java NavigableSet Class

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

The headset() method returns elements up to a limit defined as a parameter.First, create a NavigableSet and add elements −NavigableSet set = new TreeSet(); set.add(10); set.add(25); set.add(40); set.add(55); set.add(70); set.add(85); set.add(100);Now, use the headset() method −set.headSet(55));The following is an example −Exampleimport java.util.NavigableSet; import java.util.TreeSet; public class Demo {    public static void main(String[] args) {       NavigableSet set = new TreeSet();       set.add(10);       set.add(25);       set.add(40);       set.add(55);       set.add(70);       set.add(85);       set.add(100);       System.out.println("Returned Value = " + set.headSet(55)); ...

Read More

Area of circle which is inscribed in an equilateral triangle?

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 10K+ Views

The area of a circle inscribed inside an equilateral triangle is found using the mathematical formula πa2/12.Lets see how this formula is derived, Formula to find the radius of the inscribed circle = area of the triangle / semi-perimeter of triangle.Area of triangle of side a = (√3)a2/4Semi-perimeter of triangle of side a = 3a/2According to formula, Radius of circle = (√3)a22/4 / 3a/2 = a/2√3Area of circle = πr2 = πa2/12Example Code#include int main(void) {    int a = 5;    float pie = 3.14;    float area = (float)((pie*a*a)/12);    printf("the area of circle inscribed in the ...

Read More

Copy all elements of Java LinkedHashSet to an Object Array

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

First, create a LinkedHashSet and add elements −LinkedHashSet l = new LinkedHashSet(); l.add(new String("1")); l.add(new String("2")); l.add(new String("3")); l.add(new String("4")); l.add(new String("5")); l.add(new String("6")); l.add(new String("7"));Now, copy it to an object array like this −// copying Object[] arr = l.toArray();The following is an example to copy all elements of a LinkedHashSet to an object array −Exampleimport java.util.*; public class Demo { public static void main(String[] args) { LinkedHashSet l = new LinkedHashSet(); l.add(new String("1")); l.add(new String("2")); ...

Read More

Area of Circumcircle of a Right Angled Triangle?

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 1K+ Views

The area of circumcircle of a right-angle triangle when the hypotenuse(H) of the triangle is given is found using the formula πH2/4.This formula is derived using the fact that the circumcircle touches all the corners of the triangle, in this case the maximum length between two points in the hypotheses which passes through the center of the circle. This makes the hypotenuse the diameter of the circle.This is why the area of circle which is πd2/4. (d = 2r) replaces the d by H.ExampleHypotenuse = 8Area of circle = 50.26Example Code#include int main(void) {    int H = 14; ...

Read More

C++ Program to Find Lowest Common Ancestor in a Binary Search Tree

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

A binary tree which has at most two children, specified as left child and right child. This is a C++ Program to find the lowest common ancestor in a Binary Tree.AlgorithmBegin Create a structure n to declare data d, a left child pointer l and a right child pointer r.    Create a function to create newnode. Call a function LCA() to Find lowest common ancestor in a binary tree:    Assume node n1 and n2 present in the tree.    If root is null, then return.       If root is not null there are two cases.   ...

Read More

A comma operator question in C/C++ ?

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

Comma Operator in C/C++ programming language has two contexts −As a Separator −As an operator − The comma operator { , } is a binary operator that discards the first expression (after evaluation) and then use the value of the second expression. This operator has the least precedence.Consider the following codes and guess the output −Example#include int main(void) {    char ch = 'a', 'b', 'c';    printf("%c", ch);    return 0; }OutputIt gives an error because the works as a separator.prog.c: In function ‘main’: prog.c:5:20: error: expected identifier or ‘(’ before 'b' char ch = 'a', 'b', 'c'; ...

Read More

Create a new ArrayList from another collection in Java

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

An ArrayList can be created from another collection using the java.util.Arrays.asList() method. A program that demonstrates this is given as follows −Exampleimport java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Demo {    public static void main(String args[]) throws Exception {       String str[] = { "John", "Macy", "Peter", "Susan", "Lucy" };       List aList = new ArrayList(Arrays.asList(str));       System.out.println("The ArrayList elements are: " + aList);    } }OutputThe ArrayList elements are: [John, Macy, Peter, Susan, Lucy]Now let us understand the above program.The string array str[] is defined. Then an ArrayList is created using the ...

Read More

How to populate a 2d array with random alphabetic values from a range in Java?

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 1K+ Views

To populate a 2d array with random alphabets, use the Random class. Let us first declare a 2d array −char arr[][] = new char[3][3];Now, in a nested for loop, use the Random class object to get random values on the basis of switch case. Here, our range is 3 i.e. set of 3 alphabets at once −Random randNum = new Random(); for (int i = 0; i < 3; i++) {    for (int j = 0; j < 3; j++) {       int x = randNum.nextInt(3);       switch (x) {          case ...

Read More

Java Program to sort a subset of array elements

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

Let us first create a string array −String[] strArr = new String[] { "r", "p", "v", "y", "s", "q" };Now, use Arrays.sort() to get the subset. Use the following to sort only from the index range 2 to 6.Arrays.sort(strArr, 2, 6);Exampleimport java.util.Arrays; public class Demo {    public static void main(String[] args) {       String[] strArr = new String[] { "r", "p", "v", "y", "s", "q" };       Arrays.sort(strArr, 2, 6);       System.out.println("Sorted subset of array elements from index 2 to 6...");       for (int a = 0; a < strArr.length; a++) ...

Read More

Java Program to create an array with randomly shuffled numbers in a given range

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

For randomly shuffled numbers, let us create an integer list and add some numbers −List < Integer > list = new ArrayList < Integer > (); list.add(10); list.add(50); list.add(100); list.add(150); list.add(200);Now, shuffle the elements for random values −Collections.shuffle(list);Create an int array with the same number of elements in the above list −intlen = list.size(); int[] res = newint[len];Set the shuffled values in the new array −for (inti = 0; i < len; i++) {    res[i] = list.get(i); }Exampleimport java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class Demo {    public static void main(String args[]) {       ...

Read More
Showing 131–140 of 1,421 articles
« Prev 1 12 13 14 15 16 143 Next »
Advertisements