├── BasicTopics
├── .gitignore
├── .idea
│ ├── .gitignore
│ ├── inspectionProfiles
│ │ └── Project_Default.xml
│ ├── kotlinc.xml
│ ├── libraries
│ │ └── KotlinJavaRuntime.xml
│ ├── misc.xml
│ ├── modules.xml
│ └── vcs.xml
├── BasicTopics.iml
└── src
│ └── main
│ └── kotlin
│ ├── Array
│ ├── Problems
│ │ ├── AverageOfElements.kt
│ │ ├── MaximumAndMinimum.kt
│ │ ├── SearchElement.kt
│ │ └── SortElements.kt
│ └── arrayIntro.kt
│ ├── ConditionalStatement
│ ├── Exercise
│ │ ├── EvenOrOdd.kt
│ │ ├── LeapYear.kt
│ │ ├── Palindrome.kt
│ │ └── VowelOrConsonant.kt
│ ├── IfElse.kt
│ ├── WhenStatement.kt
│ └── whenStatementExample.kt
│ ├── ConsoleProjects
│ ├── Calculator
│ │ ├── Calc.kt
│ │ └── Main.kt
│ └── GuessingGame
│ │ ├── GuessingGame.kt
│ │ └── Main.kt
│ ├── Function
│ ├── Exercises
│ │ └── BinarySearch.kt
│ └── Intro
│ │ └── FunctionIntro.kt
│ ├── Loop
│ ├── Exercises
│ │ ├── Factorial.kt
│ │ ├── Fibonacci.kt
│ │ └── PatternPrinting
│ │ │ └── Triangle.kt
│ ├── forLoop.kt
│ └── whileLoop.kt
│ ├── TypeCast
│ └── typeCast.kt
│ ├── VariableAndDatatypes
│ └── variableAndDatatype.kt
│ └── userInput.kt
└── README.md
/BasicTopics/.gitignore:
--------------------------------------------------------------------------------
1 | ### IntelliJ IDEA ###
2 | out/
3 | !**/src/main/**/out/
4 | !**/src/test/**/out/
5 |
6 | ### Eclipse ###
7 | .apt_generated
8 | .classpath
9 | .factorypath
10 | .project
11 | .settings
12 | .springBeans
13 | .sts4-cache
14 | bin/
15 | !**/src/main/**/bin/
16 | !**/src/test/**/bin/
17 |
18 | ### NetBeans ###
19 | /nbproject/private/
20 | /nbbuild/
21 | /dist/
22 | /nbdist/
23 | /.nb-gradle/
24 |
25 | ### VS Code ###
26 | .vscode/
27 |
28 | ### Mac OS ###
29 | .DS_Store
--------------------------------------------------------------------------------
/BasicTopics/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 | # Editor-based HTTP Client requests
5 | /httpRequests/
6 | # Datasource local storage ignored files
7 | /dataSources/
8 | /dataSources.local.xml
9 |
--------------------------------------------------------------------------------
/BasicTopics/.idea/inspectionProfiles/Project_Default.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/BasicTopics/.idea/kotlinc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/BasicTopics/.idea/libraries/KotlinJavaRuntime.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/BasicTopics/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/BasicTopics/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/BasicTopics/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/BasicTopics/BasicTopics.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/BasicTopics/src/main/kotlin/Array/Problems/AverageOfElements.kt:
--------------------------------------------------------------------------------
1 | package Array.Problems
2 |
3 | class AverageOfElements {
4 | companion object{
5 | @JvmStatic
6 | fun main(args: Array)
7 | {
8 | // Problem statement: find the average of elements in an array
9 |
10 | var n: Int
11 | print("Enter the number of elements: ")
12 | n = readLine()!!.toInt()
13 |
14 | var arr = IntArray(n)
15 | println("Enter the elements: ")
16 | for(i in 0 until n)
17 | arr[i] = readLine()!!.toInt()
18 |
19 | var sum: Float = 0.0f
20 | for(i in 0 until n)
21 | sum += arr[i]
22 |
23 | var avg:Float = sum / n
24 | println("Average of elements: $avg")
25 | }
26 | }
27 | }
--------------------------------------------------------------------------------
/BasicTopics/src/main/kotlin/Array/Problems/MaximumAndMinimum.kt:
--------------------------------------------------------------------------------
1 | package Array.Problems
2 |
3 | class MaximumAndMinimum {
4 | companion object {
5 | @JvmStatic
6 | fun main(agrs: Array)
7 | {
8 | // Problem statement: find the maximum and minimum element in an array
9 |
10 | var n: Int
11 | print("Enter the number of elements: ")
12 | n = readLine()!!.toInt()
13 |
14 | var arr = IntArray(n)
15 | println("Enter the elements: ")
16 | for(i in 0 until n)
17 | arr[i] = readLine()!!.toInt()
18 |
19 | var max = arr[0]
20 | var min = arr[0]
21 |
22 | for(i in 1 until n) {
23 | if(arr[i] > max)
24 | max = arr[i]
25 | if(arr[i] < min)
26 | min = arr[i]
27 | }
28 |
29 | println("Maximum element: $max")
30 | println("Minimum element: $min")
31 | }
32 | }
33 | }
--------------------------------------------------------------------------------
/BasicTopics/src/main/kotlin/Array/Problems/SearchElement.kt:
--------------------------------------------------------------------------------
1 | package Array.Problems
2 |
3 | class SearchElement {
4 | companion object {
5 | @JvmStatic
6 | fun main(args: Array)
7 | {
8 | // Problem statement: search an element in an array
9 |
10 | var n: Int
11 | print("Enter the number of elements: ")
12 | n = readLine()!!.toInt()
13 |
14 | var arr = IntArray(n)
15 | println("Enter the elements: ")
16 | for(i in 0 until n)
17 | arr[i] = readLine()!!.toInt()
18 |
19 | var key: Int
20 | print("Enter the element to be searched: ")
21 | key = readLine()!!.toInt()
22 |
23 | var flag = 0
24 | for(i in 0 until n)
25 | {
26 | if(arr[i] == key)
27 | {
28 | flag = 1
29 | break
30 | }
31 | }
32 |
33 | if(flag == 1)
34 | println("Element found")
35 | else
36 | println("Element not found")
37 | }
38 | }
39 | }
--------------------------------------------------------------------------------
/BasicTopics/src/main/kotlin/Array/Problems/SortElements.kt:
--------------------------------------------------------------------------------
1 | package Array.Problems
2 |
3 | class SortElements {
4 | companion object {
5 | @JvmStatic
6 | fun main(agrs: Array)
7 | {
8 | // Problem statement: sort the elements in an array
9 |
10 | var n: Int
11 | print("Enter the number of elements: ")
12 | n = readLine()!!.toInt()
13 |
14 | var arr = IntArray(n)
15 | println("Enter the elements: ")
16 | for(i in 0 until n)
17 | arr[i] = readLine()!!.toInt()
18 |
19 | var temp: Int
20 | for(i in 0 until n-1) {
21 | for(j in 0 until n-i-1) {
22 | if(arr[j] > arr[j+1]) {
23 | temp = arr[j]
24 | arr[j] = arr[j+1]
25 | arr[j+1] = temp
26 | }
27 | }
28 | }
29 |
30 | println("Sorted array: ")
31 | for(i in 0 until n)
32 | print("${arr[i]} ")
33 | }
34 | }
35 | }
--------------------------------------------------------------------------------
/BasicTopics/src/main/kotlin/Array/arrayIntro.kt:
--------------------------------------------------------------------------------
1 | package Array
2 |
3 | class arrayIntro {
4 | companion object {
5 | @JvmStatic
6 | fun main(args: Array){
7 | //Arrays are used to store multiple values in a single variable, instead of creating separate variables for each value.
8 | //To create an array, use the arrayOf() function, and place the values in a comma-separated list inside it:
9 |
10 | val favouriteFruits = arrayOf("Mango", "Banana", "Oranges", "Grapes") // initialization of array
11 | print(favouriteFruits[0]) // this is how we can traverse an array
12 |
13 | //how to show the elements then ? -> By simply using a loop which will traverse all the element
14 |
15 | var x : String
16 | for(x in favouriteFruits){
17 | println(x)
18 | }
19 | //taking user input in array
20 | print("Enter the elements")
21 | var item = IntArray(3)
22 | for (i in 0 until 3)
23 | item[i]= readLine()!!.toInt()
24 |
25 | for (i in 0 until 3)
26 | println(item[i])
27 |
28 | //there are some functions which are very useful
29 | favouriteFruits.reverse() // this will reverse the array by index
30 | println("It will print the size of the array : ${favouriteFruits.size}")
31 |
32 | }
33 | }
34 | }
--------------------------------------------------------------------------------
/BasicTopics/src/main/kotlin/ConditionalStatement/Exercise/EvenOrOdd.kt:
--------------------------------------------------------------------------------
1 | package ConditionalStatement.Exercise
2 |
3 | class EvenOrOdd {
4 | companion object {
5 | @JvmStatic
6 | fun main(args: Array) {
7 |
8 | // Problem Statement: Write a program to check whether a number is even or odd.
9 |
10 | print("Please enter a number: ")
11 | val num = readLine()!!.toInt()
12 |
13 | if(num % 2 == 0)
14 | println("$num is even.")
15 | else
16 | println("$num is odd.")
17 |
18 | }
19 | }
20 | }
--------------------------------------------------------------------------------
/BasicTopics/src/main/kotlin/ConditionalStatement/Exercise/LeapYear.kt:
--------------------------------------------------------------------------------
1 | package ConditionalStatement.Exercise
2 |
3 | class LeapYear {
4 | companion object {
5 | @JvmStatic
6 | fun main(args: Array) {
7 |
8 | // Problem Statement: Write a program to check whether a year is leap year or not.
9 |
10 | print("Please enter a year: ")
11 | val year = readLine()!!.toInt()
12 |
13 | if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
14 | println("$year is a leap year.")
15 | else
16 | println("$year is not a leap year.")
17 |
18 | }
19 | }
20 | }
--------------------------------------------------------------------------------
/BasicTopics/src/main/kotlin/ConditionalStatement/Exercise/Palindrome.kt:
--------------------------------------------------------------------------------
1 | package ConditionalStatement.Exercise
2 |
3 | class Palindrome {
4 | companion object {
5 | @JvmStatic
6 | fun main(args: Array) {
7 | // Problem statement: check whether a string is palindrome or not
8 |
9 | var str: String
10 | print("Enter the string: ")
11 | str = readLine()!!
12 |
13 | var rev = str.reversed()
14 | if (str == rev)
15 | println("Palindrome")
16 | else
17 | println("Not Palindrome")
18 | }
19 | }
20 | }
--------------------------------------------------------------------------------
/BasicTopics/src/main/kotlin/ConditionalStatement/Exercise/VowelOrConsonant.kt:
--------------------------------------------------------------------------------
1 | package ConditionalStatement.Exercise
2 |
3 | class VowelOrConsonant
4 | {
5 | companion object
6 | {
7 | @JvmStatic
8 | fun main(args: Array)
9 | {
10 | print("Enter an alphabet: ")
11 | val ch: Char = readLine()!!.single().toLowerCase()
12 |
13 | if((ch in 'a'..'z') || (ch in 'A'..'z'))
14 | {
15 | when(ch)
16 | {
17 | 'a', 'e', 'i', 'o', 'u' -> print("$ch is vowel.")
18 | else -> print("$ch is consonant.")
19 | }
20 | }
21 | else {
22 | println("Invalid input.")
23 | }
24 | }
25 | }
26 | }
--------------------------------------------------------------------------------
/BasicTopics/src/main/kotlin/ConditionalStatement/IfElse.kt:
--------------------------------------------------------------------------------
1 | package ConditionalStatement
2 |
3 | class ifElse {
4 | companion object {
5 | @JvmStatic
6 | fun main(args: Array) {
7 | var num = 10
8 | // In case of handeling conditional problem we can use If-else, lets see an example, a problem of finding the odd numbers.
9 | if(num > 0) {
10 | print("Positive number")
11 | } else if( num < 0){
12 | print("Negative number")
13 | } else {
14 | print("Non-negative number")
15 | }
16 | // In case we have one line after the if statement we can ignore using braces.
17 |
18 | if(num > 0) print("Positive number")
19 | else if( num < 0) print("Negative number")
20 | else print("Non-negative number")
21 |
22 |
23 | }
24 | }
25 | }
--------------------------------------------------------------------------------
/BasicTopics/src/main/kotlin/ConditionalStatement/WhenStatement.kt:
--------------------------------------------------------------------------------
1 | package ConditionalStatement
2 |
3 | class whenStatement {
4 | companion object {
5 | @JvmStatic
6 | fun main(args: Array) {
7 | val day = 5
8 |
9 | val result = when (day) {
10 | 1 -> "Monday"
11 | 2 -> "Tuesday"
12 | 3 -> "Wednesday"
13 | 4 -> "Thursday"
14 | 5 -> "Friday"
15 | 6 -> "Saturday"
16 | 7 -> "Sunday"
17 | else -> "Invalid day."
18 | }
19 | println(result)
20 | }
21 | }
22 | }
--------------------------------------------------------------------------------
/BasicTopics/src/main/kotlin/ConditionalStatement/whenStatementExample.kt:
--------------------------------------------------------------------------------
1 | package ConditionalStatement
2 | //
3 | //class whenStatementExample {
4 | // companion object {
5 | // @JvmStatic
6 | // fun main(args: Array): Int {
7 | // println("Welcome to the world of calculation !!")
8 | // print("1. Add\n2. Subtract\n3.Multiply \n4. Divition\nSelect option :")
9 | // var option = readLine()
10 | //// if (option != null){
11 | //// option.toInt()
12 | //// when(option){
13 | //// 1 -> {}
14 | //// }
15 | ////
16 | //// }
17 | //
18 | // }
19 | // }
20 | //}
--------------------------------------------------------------------------------
/BasicTopics/src/main/kotlin/ConsoleProjects/Calculator/Calc.kt:
--------------------------------------------------------------------------------
1 | package ConsoleProjects.Calculator
2 |
3 | class Calc {
4 | fun add(a: Float, b: Float): Float {
5 | return a + b
6 | }
7 | fun sub(a: Float, b: Float): Float {
8 | return a - b
9 | }
10 | fun mul(a: Float, b: Float): Float {
11 | return a * b
12 | }
13 | fun divide(a: Float, b: Float): Float {
14 | return a / b
15 | }
16 | fun showInitialOptions() {
17 | println("1. Addition")
18 | println("2. Subtraction")
19 | println("3. Multiplication")
20 | println("4. Division")
21 | println("5. Exit")
22 | }
23 | }
--------------------------------------------------------------------------------
/BasicTopics/src/main/kotlin/ConsoleProjects/Calculator/Main.kt:
--------------------------------------------------------------------------------
1 | package ConsoleProjects.Calculator
2 |
3 | class Main {
4 | companion object {
5 | @JvmStatic
6 | fun main(agrs: Array)
7 | {
8 | println("Welcome to the calculator!!!")
9 | var calc = Calc()
10 | var choice: Int
11 | var a: Float
12 | var b: Float
13 |
14 | do {
15 | calc.showInitialOptions()
16 | print("Enter your choice: ")
17 | choice = readLine()!!.toInt()
18 |
19 | if(choice == 5){
20 | println("Thank you for using the calculator!!!")
21 | println("Exiting...")
22 | break
23 | }
24 | if(choice < 1 || choice > 5){
25 | println("Invalid choice")
26 | continue
27 | }
28 |
29 | print("Enter the first number: ")
30 | a = readLine()!!.toFloat()
31 | print("Enter the second number: ")
32 | b = readLine()!!.toFloat()
33 |
34 | when(choice) {
35 | 1 -> println("Sum of $a and $b is ${calc.add(a, b)}")
36 | 2 -> println("Difference of $a and $b is ${calc.sub(a, b)}")
37 | 3 -> println("Product of $a and $b is ${calc.mul(a, b)}")
38 | 4 -> println("Division of $a and $b is ${calc.divide(a, b)}")
39 | else -> println("Invalid choice")
40 | }
41 | } while(choice != 5)
42 |
43 | }
44 | }
45 | }
--------------------------------------------------------------------------------
/BasicTopics/src/main/kotlin/ConsoleProjects/GuessingGame/GuessingGame.kt:
--------------------------------------------------------------------------------
1 | package ConsoleProjects.GuessingGame
2 |
3 | class GuessingGame {
4 | fun startGame() {
5 | var guess: Int
6 | var number: Int
7 | var count = 0
8 | var playAgain: String
9 |
10 | do {
11 | number = (Math.random() * 100).toInt()
12 | println("Guess a number between 0 and 100")
13 |
14 | do {
15 | print("Enter your guess: ")
16 | guess = readLine()!!.toInt()
17 | count++
18 | if(guess > number) {
19 | println("Too high")
20 | } else if(guess < number) {
21 | println("Too low")
22 | } else {
23 | println("You got it!!!")
24 | println("You took $count guesses")
25 | }
26 | } while(guess != number)
27 | print("Do you want to play again? (y/n): ")
28 | playAgain = readLine()!!.toLowerCase()
29 |
30 | } while(playAgain == "y")
31 | }
32 | }
--------------------------------------------------------------------------------
/BasicTopics/src/main/kotlin/ConsoleProjects/GuessingGame/Main.kt:
--------------------------------------------------------------------------------
1 | package ConsoleProjects.GuessingGame
2 |
3 | class Main {
4 | companion object {
5 | @JvmStatic
6 | fun main(agrs: Array)
7 | {
8 | var game = GuessingGame()
9 | game.startGame()
10 | }
11 | }
12 | }
--------------------------------------------------------------------------------
/BasicTopics/src/main/kotlin/Function/Exercises/BinarySearch.kt:
--------------------------------------------------------------------------------
1 | package Function.Exercises
2 |
3 | class BinarySearch {
4 | companion object {
5 |
6 | // Problem statement: find the index of an element in an array using binary search
7 |
8 | fun BinarySearch(arr: IntArray, low: Int, high: Int, key: Int): Int {
9 |
10 | var low = low
11 | var high = high
12 | while (low <= high) {
13 | val mid = (high + low) / 2
14 | if (arr[mid] == key)
15 | return mid
16 | if (arr[mid] < key)
17 | low = mid + 1
18 | else
19 | high = mid - 1
20 | }
21 | return -1
22 | }
23 |
24 | @JvmStatic
25 | fun main(args: Array)
26 | {
27 |
28 | var n:Int
29 | print("Enter the number of elements: ")
30 | n = readLine()!!.toInt()
31 |
32 | var arr = IntArray(n)
33 | println("Enter array elements: ")
34 | for(i in 0 until n)
35 | arr[i] = readLine()!!.toInt()
36 |
37 | var key: Int
38 | print("Enter the key/value to search: ")
39 | key = readLine()!!.toInt()
40 |
41 | var index = BinarySearch(arr, 0, n-1, key)
42 | if(index == -1)
43 | println("Element not found")
44 | else
45 | println("Element found at index $index")
46 | }
47 | }
48 | }
--------------------------------------------------------------------------------
/BasicTopics/src/main/kotlin/Function/Intro/FunctionIntro.kt:
--------------------------------------------------------------------------------
1 | package Function.Exercises
2 |
3 | class functionIntro {
4 | //A function is a block of code which only runs when it is called.
5 | //You can pass data, known as parameters, into a function.
6 | //Functions are used to perform certain actions, and they are also known as methods.
7 | // Predefined Functions :
8 | // So it turns out you already know what a function is. You have been using it the whole time through this tutorial!
9 | // For example, println() is a function. It is used to output/print text to the screen:
10 |
11 |
12 | }
--------------------------------------------------------------------------------
/BasicTopics/src/main/kotlin/Loop/Exercises/Factorial.kt:
--------------------------------------------------------------------------------
1 | package Loop.Exercises
2 |
3 | class Factorial {
4 | companion object {
5 | @JvmStatic
6 | fun main(args: Array)
7 | {
8 |
9 | // Problem Statement: Write a program to find the factorial of a number.
10 |
11 | print("Please enter a number: ")
12 | val num = readLine()!!.toInt()
13 |
14 | var factorial = 1
15 |
16 | if( num >= 0)
17 | {
18 | for(i in 1..num)
19 | {
20 | factorial *= i
21 | }
22 | println("Factorial of $num is $factorial")
23 | } else {
24 | println("Please enter a positive number.")
25 | }
26 |
27 | }
28 | }
29 | }
--------------------------------------------------------------------------------
/BasicTopics/src/main/kotlin/Loop/Exercises/Fibonacci.kt:
--------------------------------------------------------------------------------
1 | package Loop.Exercises
2 |
3 | class Fibonacci {
4 | companion object {
5 | @JvmStatic
6 | fun main(agrs: Array)
7 | {
8 | // Problem statement: find the first n Fibonacci numbers
9 |
10 | var n: Int
11 | print("Enter the number of Fibonacci numbers: ")
12 | n = readLine()!!.toInt()
13 |
14 | var a: Long = 0
15 | var b: Long = 1
16 | var c: Long
17 |
18 | print("First $n Fibonacci numbers: ")
19 | when(n) {
20 | 1 -> print("$a ")
21 | 2 -> print("$a $b ")
22 | else -> {
23 | print("$a $b ")
24 | for (i in 3..n) {
25 | c = a + b
26 | print("$c ")
27 | a = b
28 | b = c
29 | }
30 | }
31 | }
32 | }
33 | }
34 | }
--------------------------------------------------------------------------------
/BasicTopics/src/main/kotlin/Loop/Exercises/PatternPrinting/Triangle.kt:
--------------------------------------------------------------------------------
1 | package Loop.Exercises.PatternPrinting
2 |
3 | class Triangle {
4 | companion object {
5 | @JvmStatic
6 | fun main(agrs: Array)
7 | {
8 | // Problem statement: Print a triangle of stars
9 |
10 | var n: Int = 0
11 |
12 | do{
13 | print("Please enter the number of rows n (where n>1): ")
14 | n = readLine()!!.toInt()
15 | }
16 | while (n < 2)
17 |
18 | for (i in 1..n) {
19 | for (j in 1..i) {
20 | print("* ")
21 | }
22 | println()
23 | }
24 |
25 | }
26 | }
27 | }
--------------------------------------------------------------------------------
/BasicTopics/src/main/kotlin/Loop/forLoop.kt:
--------------------------------------------------------------------------------
1 | package Loop
2 |
3 | class forLoop {
4 | companion object{
5 | @JvmStatic
6 | fun main(args: Array){
7 | //for is another kind of loop, we can use this in many ways depend on the requirements
8 |
9 | var item = IntArray(3)
10 |
11 | println("Enter elements")
12 | for (i in 0 until 3)
13 | item[i]= readLine()!!.toInt()
14 |
15 | for (x in item)
16 | println(x)
17 |
18 | if (10 in item) print("Present")
19 |
20 | }
21 | }
22 |
23 |
24 | }
--------------------------------------------------------------------------------
/BasicTopics/src/main/kotlin/Loop/whileLoop.kt:
--------------------------------------------------------------------------------
1 | package Loop
2 |
3 | class whileLoop {
4 | /*
5 | Loops can execute a block of code as long as a specified condition is reached.Loops are handy because they save time,
6 | reduce errors, and they make code more readable.
7 | */
8 | companion object {
9 | @JvmStatic
10 | /*
11 | if we want to print the number serially then we can use loop for that
12 | */
13 | fun main(args: Array) {
14 |
15 | var i = 0// initiate the counter
16 | while (i < 5) { // set a braking condition
17 | println(i)
18 | i++ // incrementation
19 | }
20 | }
21 | }
22 | }
--------------------------------------------------------------------------------
/BasicTopics/src/main/kotlin/TypeCast/typeCast.kt:
--------------------------------------------------------------------------------
1 | package TypeCast
2 |
3 | class typeCast {
4 | companion object {
5 | @JvmStatic
6 | fun main(args: Array) {
7 | /*
8 | Type Cast :-
9 |
10 | To convert a numeric data type to another type, you must use one of the following functions:
11 | --toByte(), toShort(), toInt(), toLong(), toFloat(), toDouble() or toChar()
12 | */
13 | var x = 10
14 | var y : String = x.toString()
15 | println(y +" "+ x)
16 |
17 | //User Input:
18 |
19 | val age = readlnOrNull()?.toInt() // here the input is taken and converted into integer
20 | print(age)
21 |
22 | }
23 | }
24 | }
--------------------------------------------------------------------------------
/BasicTopics/src/main/kotlin/VariableAndDatatypes/variableAndDatatype.kt:
--------------------------------------------------------------------------------
1 | package VariableAndDatatypes
2 |
3 | class variableAndDatatype {
4 | companion object {
5 | @JvmStatic
6 | fun main(args: Array) {
7 | println("Hello World!")
8 |
9 |
10 | /*To create a variable, use var or val. The difference between var and val is that variables
11 | declared with the var keyword can be changed/modified, while val variables cannot */
12 |
13 | var firstName = "pritom"
14 | val lastName = "debnath"
15 | println("My name is $firstName $lastName")
16 |
17 | firstName="pratik"
18 | //lastName = "Pandit"
19 | // As lastName is declared val it can not be modified, you can try to uncomment line 12 and check it
20 | println("My name is $firstName $lastName")
21 |
22 | /*The general rule for Kotlin variables are:
23 |
24 | -Names can contain letters, digits, underscores, and dollar signs
25 | -Names should start with a letter
26 | -Names are case sensitive ("myVar" and "myvar" are different variables)
27 | -Names should start with a lowercase letter and it cannot contain whitespace
28 | -Reserved words (like Kotlin keywords, such as var or String) cannot be used as names
29 | */
30 |
31 |
32 | /*kotlin can detect the input, so mentioning the data type is not mandatory if value is assigned. In case
33 | value is not assigned while declaring then data type should be mentioned*/
34 |
35 | var age: Int
36 | age = 23
37 | println("My age is $age")
38 |
39 | /*Data types are divided into different groups:
40 | -Numbers
41 | -Characters
42 | -Booleans
43 | -Strings
44 | -Arrays
45 | */
46 |
47 | /*
48 | Number types are divided into two groups:
49 | -Integer types store whole numbers, positive or negative (such as 123 or -456), without decimals.
50 | Valid types are Byte, Short, Int and Long.
51 | -Floating point types represent numbers with a fractional part, containing one or more decimals.
52 | There are two types: Float and Double.
53 | */
54 |
55 | val intNum: Int = 100000
56 | println("Integer number $intNum")
57 |
58 | val longNum: Long = 15000000000L
59 | println("Long number "+longNum)
60 |
61 | println("Result ${intNum + longNum}")
62 |
63 | /*
64 | Use Float or Double?
65 | The precision of a floating point value indicates how many digits the value can have after the decimal point.
66 | The precision of Float is only six or seven decimal digits, while Double variables have a precision of about 15 digits.
67 | Therefore, it is safer to use Double for most calculations.
68 |
69 | Also note that you should end the value of a Float type with an "F".
70 |
71 | */
72 |
73 | var floatNum : Float = 3.5F
74 | println("Float Number :$floatNum")
75 | //A floating point number can also be a scientific number with an "e" or "E" to indicate the power of 10:
76 | var sciNum = 3.5E3F
77 | println("Scientific Float Number :$sciNum")
78 | //Boolean
79 | var isThisTrue = true
80 | println("Is this true :$isThisTrue")
81 |
82 | //Char
83 | val myGrade: Char = 'B'
84 | println("My grade is :$myGrade")
85 |
86 |
87 | }
88 | }
89 | }
--------------------------------------------------------------------------------
/BasicTopics/src/main/kotlin/userInput.kt:
--------------------------------------------------------------------------------
1 | class userInput {
2 | companion object{
3 | @JvmStatic
4 | fun main(args: Array){
5 | print("What is your name? ")
6 | var input = readln() // it will take user input as string and it is not null supported , and by type cast it can be converted
7 | println("My name is $input")
8 | print("Type a number ")
9 | var num = readLine()!!.toInt() // it is nullable and takes input as a string
10 | print("$num + 10 is : ${num+10}")
11 | }
12 | }
13 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Learn Kotlin
2 | - Variable and datatype
3 | - Type casting and Operators
4 |
--------------------------------------------------------------------------------