#include <math.h> #include <stdio.h> void main() { double a, b, c, d, root1, root2, realPart, imagPart; printf("Enter coefficients a, b and c: "); scanf("%lf %lf %lf", &a, &b, &c); d = b * b - 4 * a * c; if (d > 0) { root1 = (-b + sqrt(d)) / (2 * a); root2 = (-b - sqrt(d)) / (2 * a); printf("root1 = %.2lf and root2 = %.2lf", root1, root2); } else if (d == 0) { root1 = root2 = -b / (2 * a); printf("root1 = root2 = %.2lf;", root1); } else { realPart = -b…
-
-
Kotlin has powerful type inference. While you can explicitly declare the type of a variable, you’ll usually let the compiler do the work by inferring it. Kotlin does not enforce immutability, though it is recommended. In essence use val over var. var a: String = "initial" println(a) val b: Int = 1 val c = 3 Declares a mutable variable and initializing it. Declares an immutable variable and initializing it. Declares an immutable variable and initializing it without specifying the type. The compiler infers the type Int. var e: Int println(e) Declares a variable without initialization. An attempt to use the variable causes…
-
Varargs allow you to pass any number of arguments by separating them with commas. The vararg modifier turns a parameter into a vararg. This allows calling printAll with any number of string arguments. Thanks to named parameters, you can even add another parameter of the same type after the vararg. This wouldn’t be allowed in Java because there’s no way to pass a value. Using named parameters, you can set a value to prefix separately from the vararg. At runtime, a vararg is just an array. To pass it along into a vararg parameter, use the special spread operator *…
-
Certain functions can be “upgraded” to operators, allowing their calls with the corresponding operator symbol. This takes the infix function from above one step further using the operator modifier. The operator symbol for times() is * so that you can call the function using 2 * “Bye”. An operator function allows easy range access on strings. The get() operator enables bracket-access syntax. operator fun Int.times(str: String) = str.repeat(this) println(2 * "Bye ") operator fun String.get(range: IntRange) = substring(range) val str = "Always forgive your enemies; nothing annoys them so much." println(str[0..14])
-
Member functions and extensions with a single parameter can be turned into infix functions. Defines an infix extension function on Int. Calls the infix function. Creates a Pair by calling the infix function to from the standard library. Here’s your own implementation of to creatively called onto. Infix notation also works on members functions (methods). The containing class becomes the first parameter. Note that the example uses local functions (functions nested within another function). fun main() { infix fun Int.times(str: String) = str.repeat(this) println(2 times "Bye ") val pair = "Ferrari" to "Katrina" println(pair) infix fun String.onto(other: String) = Pair(this,…
-
A simple function that takes a parameter of type String and returns Unit (i.e., no return value). A function that takes a second optional parameter with default value Info. The return type is omitted, meaning that it’s actually Unit. A function that returns an integer. A single-expression function that returns an integer (inferred). Calls the first function with the argument Hello. Calls the function with two parameters, passing values for both of them. Calls the same function omitting the second one. The default value Info is used. Calls the same function using named argument and changing the order of the arguments. Prints…
-
fun main() { println("Hello, World!") } An entry point to a Kotlin application is the main function. In Kotlin 1.3, you can declare main without any parameters. The return type is not specified, which means that the function returns nothing. println writes a line to the standard output. It is imported implicitly. Also note that semicolons are optional. In Kotlin versions earlier than 1.3, the main function must have a parameter of type Array<String>. fun main(args: Array<String>) { println("Hello, World!") }
-
I face this difficulty. In my case when i click on textview which is part of list-view’s custom XML file then the position is right, but when i click on the button which is the part of list-view’s custom XML file then it always gives first position. After i have to decided to perform automatic onItemClick and store all the values of clicked position into another declared variable which is below. See the image i used kotlin language instead of java. I stored all the map values into another variable which is declared above with public access specifier. [Now when i perform operation on Button click i…
-
https://www.youtube.com/watch?v=rEVWb_8XmLU Error occurred during initialization of VM Could not reserve enough space for 1572864KB object heap. To solve This Error You Have To Change Only Heap Size Which Is Define In Gradle.Properties file. You Have To Change The HeapSize To 1024m And Rebuild the Project, If The Error Is Not Solved Then You Have To Close The Project And Rebuild It.
-
Run Length Encoding is a form of lossless data compression in which runs of data are stored as a single data value and count, rather than as the original run. import java.util.*; class RLEChallange { public static void main(String args[]) { Scanner sc=new Scanner(System.in); String strin,strout=""; int count,i,j; System.out.println("Enter String "); strin=sc.nextLine(); i=0; j=0; while(i<strin.length()) { count=1; j=i+1; while(j<strin.length() && strin.toLowerCase().charAt(i)==strin.toLowerCase().charAt(j)) { count++; j++; } strout=strout+count+strin.charAt(i); i=j; } System.out.println("\n\nInPut "+strin); System.out.println("OutPut "+strout); } } Output Enter String AABCCAAA InPut AABCCAAA OutPut 2A1B2C3A Enter String AAAVVVTYUUUIIIII InPut AAAVVVTYUUUIIIII OutPut 3A3V1T1Y3U5I