Difference Between int and long in C++

Samual Sam
Updated on 26-Jun-2020 09:20:19

2K+ Views

intThe datatype int is used to store the integer values. It could be signed or unsigned. The datatype int is of 32-bit or 4 bytes. It requires less memory area than long to store a value. The keyword “int” is used to declare an integer variable.The following is the syntax of int datatype.int variable_name;Here,variable_name − The name of variable given by user.The following is an example of int datatype.Example Live Demo#include using namespace std; int main() {    int a = 8;    int b = 10;    int c = a+b;    cout

Create a Copy of a TimeZone Object in Java

Ankith Reddy
Updated on 26-Jun-2020 09:17:25

124 Views

In order to get a copy of a TimeZone object in Java, we use the clone() method. The clone() method creates of a copy of the TimeZone.Declaration −The java.util.TimeZone.clone() method is declared as follows −public Object clone()Let us see a Java program which creates a copy of the TimeZone object using the clone() method −Example Live Demoimport java.util.*; public class Example {    public static void main( String args[] ) {       // creating object of TimeZone       TimeZone obj = TimeZone.getDefault();       System.out.println("Initial object: " + obj);       // copying the TimeZone ... Read More

Find Maximum Element of ArrayList with Java Collections

Arjun Thakur
Updated on 26-Jun-2020 09:16:38

13K+ Views

In order to compute maximum element of ArrayList with Java Collections, we use the Collections.max() method. The java.util.Collections.max() returns the maximum element of the given collection. All elements must be mutually comparable and implement the comparable interface. They shouldn’t throw a ClassCastException.Declaration −The Collections.max() method is declared as follows −public static T max(Collection c)where c is the collection object whose maximum is to be found.Let us see a program to find the maximum element of ArrayList with Java collections −Example Live Demoimport java.util.*; public class Example {    public static void main (String[] args) {       List list ... Read More

Precision on a Number Format in Java

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

2K+ 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 number with 3 decimal places −29292929.988The following is the final example −Example Live Demoimport java.util.Formatter; public class Demo {     public static void main(String args[]) {        Formatter f1,  f2,  f3;        f1 = new Formatter();        f1.format("%1.3f",  29292929.98765432);        System.out.println(f1);        f2 = new Formatter();        f2.format("%1.7f",  29292929.98765432);        System.out.println(f2);   ... Read More

Add Leading Zeros to a Number in Java

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, is the number with 3 digits.String.format("%07d", val);The following is the final example.Example Live Demoimport java.util.Formatter; public class Demo {     public static void main(String args[]) {        int val = 290;        System.out.println("Integer: "+val);        String formattedStr = String.format("%07d", val);        System.out.println("With leading zeros = " + formattedStr);     } }OutputInteger: 290 With leading zeros = 0000290

Get All the IDs of the Time Zone in Java

Chandu yadav
Updated on 26-Jun-2020 09:15:09

6K+ Views

To get all the IDs of the TimeZone in Java, we use the getAvailableIDs() method. The getAvailableIDs() method returns all the available IDs which are compatible.Declaration −The java.util.TimeZone.getAvailableIDs() method is declared as follows −public static String[] getAvailableIDs()Let us see a Java program which gets all the IDs of the Time Zone:Example Live Demoimport java.util.*; public class Example {    public static void main(String args[]) {       // getting available supported ids       String[] id = TimeZone.getAvailableIDs();       // printing available ids       System.out.println("The available IDs are as follows:");       for (int ... Read More

Get IDs According to Given Time Zone Offset in Java

George John
Updated on 26-Jun-2020 09:14:08

272 Views

In order to get the IDs according to the given time zone offset in Java, we use the getAvailableIDs(int rawOffset) method. The java.util.TimeZone.getAvailableIDs(int rawOffset) method returns the available IDs according to the given time zone offset in the arguments.Declaration − The java.util.TimeZone.getAvailableIDs(int rawOffset) method is declared as follows −public static String[] getAvailableIDs(int rawOffset)where rawOffset is the given time zone GMT offset.Let us see a Java program which gets the IDs according to the given time zone offset −Example Live Demoimport java.util.*; public class Example {    public static void main(String args[]) {       // getting available supported ids for ... Read More

Catch Divide by Zero Error in C++

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

773 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

Get the Default Time Zone in Java

Chandu yadav
Updated on 26-Jun-2020 09:13:13

2K+ Views

In order to get the default Time Zone in Java, we use the getDefault() method. The java.util.TimeZone.getDefault() method returns the default TimeZone for the particular host. The source of the default TimeZone varies with the implementation.Declaration −The java.util.TimeZone.getDefault() method is declared as follows −public static TimeZone getDefault()Let us see a program to get the default time zone in Java −Example Live Demoimport java.util.*; public class Example {    public static void main( String args[] ) {       // creating default object of TimeZone       TimeZone obj = TimeZone.getDefault();       System.out.println("Default timezone object: " + obj); ... Read More

Swap Two Variable Values Without Using Third Variable in C/C++

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

580 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;    printf("After Swapping : %d\t%d", a, b);    return 0; }OutputEnter the value of a : 23 Enter the value of b : 43 After Swapping : 4323In the above program, two variables a and b are declared and initialized dynamically at run time.int a, b; printf("Enter the value of ... Read More

Advertisements