Samual Sam has Published 2310 Articles

Java Program to add leading zeros to a number

Samual Sam

Samual Sam

Updated on 26-Jun-2020 09:15:38

20K+ Views

To add leading zeros to a number, you need to format the output. Let’s say we need to add 4 leading zeros to the following number with 3 digits.int val = 290;For adding 4 leading zeros above, we will use %07d i.e. 4+3 = 7. Here, 3, as shown above, ... Read More

How to catch a divide by zero error in C++?

Samual Sam

Samual Sam

Updated on 26-Jun-2020 09:13:25

735 Views

The following is an example to catch a divide by zero error.Example Live Demo#include using namespace std; int display(int x, int y) {    if( y == 0 ) {       throw "Division by zero condition!";    }    return (x/y); } int main () {    int a = 50;    int b = 0;    int c = 0;    try {       c = display(a, b);       cout

Swapping two variable value without using third variable in C/C++

Samual Sam

Samual Sam

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

540 Views

The following is an example of swapping two variables.Example Live Demo#include int main() {    int a, b;    printf("Enter the value of a : ");    scanf("%d", &a);    printf("Enter the value of b : ");    scanf("%d", &b);    a += b -= a = b - a; ... Read More

Why does C++ require a cast for malloc() but C doesn't?

Samual Sam

Samual Sam

Updated on 26-Jun-2020 09:11:25

901 Views

In C language, the void pointers are converted implicitly to the object pointer type. The function malloc() returns void * in C89 standard. In earlier versions of C, malloc() returns char *. In C++ language, by default malloc() returns int value. So, the pointers are converted to object pointers using ... Read More

Format double type in Java

Samual Sam

Samual Sam

Updated on 26-Jun-2020 09:05:22

3K+ Views

Let’s say we have the following three values −double val1 = 15.546; double val2 = 25.87; double val3 = Math.PI;Let us now format these double-type numbers. Firstly, we are formatting Euler’s number with Math.exp(). After that, we have also evaluated log. The %.3f you can see here is what we ... Read More

Write a power (pow) function using C++

Samual Sam

Samual Sam

Updated on 26-Jun-2020 09:02:50

2K+ Views

The power function is used to find the power given two numbers that are the base and exponent. The result is the base raised to the power of the exponent.An example that demonstrates this is as follows −Base = 2 Exponent = 5 2^5 = 32 Hence, 2 ... Read More

Java Program to convert Double into String using toString() method of Double class

Samual Sam

Samual Sam

Updated on 26-Jun-2020 09:00:12

138 Views

Let us first declare a Double −Double ob = new Double(99.12);Use the toString() method to convert Double into String.String res = ob.toString();The following is the complete example.Example Live Demopublic class Demo {    public static void main(String args[]) {       Double ob = new Double(99.12);       String res = ob.toString();       System.out.println(res);     } }Output99.12Read More

Apply Ternary operator on double value in Java

Samual Sam

Samual Sam

Updated on 26-Jun-2020 08:58:33

513 Views

The ternary operator is also known as the conditional operator. This operator consists of three operands and is used to evaluate Boolean expressions.Let’s say we have the following two double values.double val1 = 20.0; double val2 = 3.7;Now, let us use the ternary operator to check for the first value.System.out.println(val1 ... Read More

Unix filename pattern matching in Python

Samual Sam

Samual Sam

Updated on 26-Jun-2020 08:57:31

2K+ Views

Here we will see how we can get the UNIX shell style pattern matching techniques using Python. There is a module called fnmatch, which is used to do the work. This module is used to compare file name against a pattern, then returns True or False according to the matches.To ... Read More

Double isInfinite() method in Java

Samual Sam

Samual Sam

Updated on 26-Jun-2020 08:51:51

132 Views

The Double isInfinite() method returns true if this Double value is infinitely large in magnitude, false otherwise.Let’s say we have the following Double values.Double val1 = new Double(3/0.); Double val2 = new Double(0/0.);Use the isInfinite() method now. The return value is a boolean.val1.isInfinite(); val2.isInfinite();The following is the final example.Example Live Demopublic ... Read More

Advertisements