Programming Articles

Page 1557 of 2547

Get the last element from a Sorted Set in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 3K+ Views

To create a Sorted Set, firstly create a Set −Set s = new HashSet();Add elements to the above set −int a[] = {77, 23, 4, 66, 99, 112, 45, 56, 39, 89}; Set s = new HashSet(); try {    for(int i = 0; i < 5; i++) {    s.add(a[i]); }After that, use TreeSet class to sort −TreeSet sorted = new TreeSet(s);Get the last element, using the last() method −System.out.println("Last element of the sorted set = "+ (Integer)sorted.last());The following is the code to get the last element from a Sorted Set in Java −Exampleimport java.util.*; public class Demo { ...

Read More

Ceil and floor functions in C++

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

The ceil FunctionThe ceil function returns the smallest possible integer value which is equal to the value or greater than that. This function is declared in “cmath” header file in C++ language. It takes single value whoes ceil value is to be calculated. The datatype of variable should be double/float/long double only.Here is the syntax of ceil function in C++ language,double ceil(double x); float ceil(float x);Here is an example of ceil function in C++ language,Example#include #include using namespace std; int main() {    float var = 1234.25;    float res;    res = ceil(var);    cout

Read More

Check whether a NavigableMap empty or not in Java

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

The isEmpty() method is used in Java to check whether a NavigableMap is empty or not.First, create a NavigableMap and add elements to it −NavigableMap n = new TreeMap(); n.put(5, "Tom"); n.put(9, "John"); n.put(14, "Jamie"); n.put(1, "Tim"); n.put(4, "Jackie"); n.put(15, "Kurt"); n.put(19, "Tiger"); n.put(24, "Jacob");Now, check whether the Map is empty or not −System.out.println("Map is empty? " + n.isEmpty());The following is an example to implement isEmpty() method and check whether the Map is empty −Exampleimport java.util.*; public class Demo {    public static void main(String[] args) {       NavigableMap n = new TreeMap();       n.put(5, "Tom"); ...

Read More

C++ Program to Check Whether Number is Even or Odd

Samual Sam
Samual Sam
Updated on 11-Mar-2026 13K+ Views

A number is even if it is divisible by two and odd if it is not divisible by two.Some of the even numbers are −2, 4, 6, 8, 10, 12, 14, 16Some of the odd numbers are −1, 3, 5, 7, 9, 11, 13, 15, 17Check Whether Number is Even or Odd using ModulusA program to check whether number is even or odd using modulus is as follows.Example#include using namespace std; int main() {    int num = 25;    if(num % 2 == 0)    cout

Read More

Const member functions in C++

Samual Sam
Samual Sam
Updated on 11-Mar-2026 20K+ Views

The const member functions are the functions which are declared as constant in the program. The object called by these functions cannot be modified. It is recommended to use const keyword so that accidental changes to object are avoided.A const member function can be called by any type of object. Non-const functions can be called by non-const objects only.Here is the syntax of const member function in C++ language, datatype function_name const();Here is an example of const member function in C++, Example#include using namespace std; class Demo {    int val;    public:    Demo(int x = 0) {   ...

Read More

How to compile assert in Java?

Smita Kapse
Smita Kapse
Updated on 11-Mar-2026 734 Views

In order to compile assert in Java, we simply set the boolean expression as false.Let us see an example program −Examplepublic class Example {    public static void main(String[] args) {       assert false;       System.out.println("Compiled and executed successfully!!!");    } }OutputCompiled and executed successfully!!!

Read More

Get Day Number of Week in Java

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

To get the day of the week, use Calendar.DAY_OF_WEEK.Firstly, declare a calendar object and get the current date and time.Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getTime().toString());Now, fetch the day of the week in an integer variable.int day = calendar.get(Calendar.DAY_OF_WEEK);The following is the final example.Exampleimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println(calendar.getTime().toString());       int day = calendar.get(Calendar.DAY_OF_WEEK);       System.out.println("Day: " + day);       int hour = calendar.get(Calendar.HOUR_OF_DAY);       System.out.println("Hour: " + hour);       int minute = calendar.get(Calendar.MINUTE); ...

Read More

Java Program to return the hexadecimal value of the supplied byte array

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

The following is the supplied byte array −byte[] b = new byte[]{'x', 'y', 'z'};We have created a custom method “display” here and passed the byte array value. The same method converts byte array to hex string −public static String display(byte[] b1){    StringBuilder strBuilder = new StringBuilder();    for(byte val : b1){     strBuilder.append(String.format("%02x", val&0xff)); } return strBuilder.toString(); }Let us see the entire example now −Examplepublic class Demo { public static void main(String args[]) { byte[] b = new byte[]{'x', 'y', 'z'}; ...

Read More

Apply modulus operator to floating-point values in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 829 Views

To apply modulus (%) operator to floating-point values in an effortless task. Let us see how.We have the following two values −double one = 9.7; double two = 1.2;Let us now apply the modulus operator −one % twoThe following is the complete example that displays the output as well −Examplepublic class Demo { public static void main(String args[]) { double one = 9.7; double two = 1.2; System.out.println( one % two ); } }Output0.09999999999999964

Read More

Enable Assertions from the command line in Java

Anvi Jain
Anvi Jain
Updated on 11-Mar-2026 568 Views

By default, assertions are disabled in Java. In order to enable them we use the following command −java -ea Example (or) java -enableassertions ExampleHere, Example is the name of the Java file.Let us see an example for generation of an assertion error by the JVM −Examplepublic class Example {    public static void main(String[] args) {       int age = 14;       assert age >= 18 : "Cannot Vote";       System.out.println("The voter's age is " + age);    } }OutputThe voter's age is 14

Read More
Showing 15561–15570 of 25,466 articles
Advertisements