Karthikeya Boyini has Published 2383 Articles

How to sum two integers without using arithmetic operators in C/C++?

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 09:27:33

170 Views

The following is an example to add two numbers without using arithmetic operators.Example Live Demo#include #include using namespace std; int add(int val1, int val2) {    while(val2 != 0) {       int c = val1 & val2;       val1 = val1 ^ val2;       val2 = c

Java Program to convert integer to hexadecimal

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 09:24:33

6K+ Views

Use the + Integer.toHexString() method in Java to convert integer to hexadecimal.Let’s say the following is our integer.int val = 768;Let us convert it to a hexadecimal value.Integer.toHexString(val)The following is the final example with the output.Example Live Demopublic class Demo {     public static void main(String[] args) {        int val = 768;        // integer   ... Read More

Where are static variables stored in C/C++?

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 09:23:06

9K+ Views

Static variables are variables that remain in memory while the program is running i.e. their lifetime is the entire program run. This is different than automatic variables as they remain in memory only when their function is running and are destroyed when the function is over.The static variables are stored ... Read More

Java Program to remove a character at a specified position

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 09:21:31

662 Views

To remove a character at a specified position, use the following logic −Let’s say the following is our string.String str = "The Haunting of Hill House!";We want to remove a character at position 7. For that, use the below logic.// removing character at position 7 int pos = 7; String ... Read More

What is the correct way to use printf to print a size_t in C/C++?

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 09:20:48

20K+ Views

We should use “%zu” to print the variables of size_t length. We can use “%d” also to print size_t variables, it will not show any error. The correct way to print size_t variables is use of “%zu”.In “%zu” format, z is a length modifier and u stand for unsigned type.The ... Read More

What is a reference variable in C++?

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 09:19:43

19K+ Views

Reference variable is an alternate name of already existing variable. It cannot be changed to refer another variable and should be initialized at the time of declaration and cannot be NULL. The operator ‘&’ is used to declare reference variable.The following is the syntax of reference variable.datatype variable_name; // variable ... Read More

How to call a parent class function from derived class function in C++?

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 09:18:28

9K+ Views

The following is an example to call parent class function from derived class function.Example Live Demo#include using namespace std; class p1 {    public:    void first() {       cout

How to use enums in C++?

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 09:17:06

10K+ Views

Enumeration is a user defined datatype in C/C++ language. It is used to assign names to the integral constants which makes a program easy to read and maintain. The keyword “enum” is used to declare an enumeration.The following is the syntax of enums.enum enum_name{const1, const2, ....... };Here, enum_name − Any ... Read More

Precision on a number format in Java

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 09:16:27

1K+ Views

You can include a precision specifier to the following format specifiers −%f %e %g %sOn floating point, the number of decimal places is known.Let’s say we declared a Formatter object −Formatter f1 = new Formatter();Now, we want 3 decimal places. For that, use 1.3f −f1.format("%1.3f", 29292929.98765432);The above will return the ... Read More

How to calculate Execution Time of a Code Snippet in C++?

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 09:14:20

2K+ Views

We can calculate the execution time of a code snippet by using following syntax −auto start = high_resolution_clock::now(); // Start time // Code snippet auto stop = high_resolution_clock::now(); // Stop time auto duration = duration_cast(stop - start); // DurationThe class high_resolution_clock is defined in “chrono” header file. The function now() ... Read More

Advertisements