To convert String to float, use the valueOf() method.Let’s say we have the following string value.String str = "0.8";Converting the string to float.Float floatVal = Float.valueOf(str).floatValue();The following is the complete example.Example Live Demopublic class Demo { public static void main(String args[]) { String str = "0.8"; Float floatVal = Float.valueOf(str).floatValue(); System.out.println("Float: "+floatVal); } }OutputFloat: 0.8
To compare Java float arrays, use the Arrays.equals() method. The return value is a boolean. Let’s say we have the following float arrays −float[] floatVal1 = new float[] { 3.2f, 5.5f, 5.3f }; float[] floatVal2 = new float[] { 8.3f, 8.8f, 9.2f }; float[] floatVal3 = new float[] { 6.2f, 6.9f, 9.9f }; float[] floatVal4 = new float[] { 3.2f, 5.5f, 5.3f };To compare them, the Arrays.equals() method is to be used −Arrays.equals(floatVal1, floatVal2); Arrays.equals(floatVal2, floatVal3); Arrays.equals(floatVal3, floatVal4);The following is the complete example wherein we compare all the arrays −Example Live Demoimport java.util.Arrays; public class Demo { public static void ... Read More
To check two float arrays for equality, use the Arrays.equals() method.In our example, we have the following two float arrays.float[] floatVal1 = new float[] { 3.2f, 5.5f, 5.3f }; float[] floatVal2 = new float[] { 3.2f, 5.5f, 5.3f };Let us now compare them for equality.if (Arrays.equals(floatVal1, floatVal2)) { System.out.println("Both are equal!"); }The following is an example wherein we compare arrays and with that also use == to check for other conditions.Example Live Demoimport java.util.Arrays; public class Demo { public static void main(String args[]) { float[] floatVal1 = new float[] { 3.2f, 5.5f, 5.3f }; ... Read More
When static keyword is used, variable or data members or functions can not be modified again. It is allocated for the lifetime of program. Static functions can be called directly by using class name.Static variables are initialized only once. Compiler persist the variable till the end of the program. Static variable can be defined inside or outside the function. They are local to the block. The default value of static variable is zero. The static variables are alive till the execution of the program.Here is the syntax of static variables in C language, static datatype variable_name = value;Here, datatype − ... Read More
The following is an example to swap strings.Example Live Demo#include #include int main() { char st1[] = "My 1st string"; char st2[] = "My 2nd string"; char swap; int i = 0; while(st1[i] != '\0') { swap = st1[i]; st1[i] = st2[i]; st2[i] = swap; i++; } printf("After swapping s1 : %s", st1); printf("After swapping s2 : %s", st2); return 0; }OutputAfter swapping s1 : My 2nd string After swapping s2 : My 1st stringIn the above program, two ... Read More
The following is an example to compute combinations using factorials.Example Live Demo#include using namespace std; int fact(int n) { if (n == 0 || n == 1) return 1; else return n * fact(n - 1); } int main() { int n, r, result; coutn; coutr; result = fact(n) / (fact(r) * fact(n-r)); cout
Here, $push can be used to add new documents in nested array. To understand the above $push concept, let us create a collection with nested array document. The query to create a collection with document is as follows:>db.nestedArrayDemo.insertOne({"EmployeeName":"Larry", "EmployeeSalary":9000, "EmployeeDetails": [{"EmployeeDOB":new Date('1990-01-21'), "EmployeeDepartment":"ComputerScience", "EmployeeProject": [{"Technology":"C", "Duration":6}, {"Technology":"Java", "Duration":7}]}]});The following is the output:{ "acknowledged" : true, "insertedId" : ObjectId("5c6d73090c3d5054b766a76e") }Now you can display documents from a collection with the help of find() method. The query is as follows:> db.nestedArrayDemo.find().pretty();The following is the output:{ "_id" : ObjectId("5c6d73090c3d5054b766a76e"), "EmployeeName" : "Larry", "EmployeeSalary" : 9000, "EmployeeDetails" ... Read More
Generally, we use header files in C/C++ languages to access the built-in functions like int, char, string functions. The function printf() is also a built-in function which is declared in “stdio.h” header file and it is used to print any kind of data on console.Here is an example to print without header files in C language, Exampleint printf(const char *text, ...); int main() { printf( "Hello World" ); return 0; }OutputHello WorldIn the above program, we printed “Hello World” without using any header file in the program by declaring the printf() function. The declaration of printf() is as ... Read More
For GregorianCalendar class, import the following package.import java.util.GregorianCalendar;Create an object.GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();Now, use the following field and add() method with a negative one (-1) to display the previous day.cal.add((GregorianCalendar.DATE), -1)Example Live Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo { public static void main(String[] a) { GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance(); System.out.println("Current date: " + cal.getTime()); // past date cal.add((GregorianCalendar.DATE), -1); System.out.println("Modified date (Previous Day): " + cal.getTime()); } }OutputCurrent date: Mon Nov 19 18:12:53 UTC 2018 Modified date (Previous Day): Sun Nov ... Read More
The function strftime() is used to format the time and date as a string. It is declared in “time.h” header file in C language. It returns the total number of characters copied to the string, if string fits in less than size characters otherwise, returns zero.Here is the syntax of strftime() in C language, size_t strftime(char *string, size_t size, const char *format, const struct tm *time_pointer)Here, string − Pointer to the destination array.size − Maximum number of characters to be copied.format − Some special format specifiers to represent the time in tm.time_pointer − Pointer to tm structure that contains the ... Read More