ako patole rra Flashcards

1
Q

Fibonacci Sequence

Write a function that prints the firstnnumbers in the Fibonacci sequence.

A

fun printFibonacci(n: Int) {
var a = 0
var b = 1

print("$a ")

for (i in 1 until n) {
    print("$b ")
    val next = a + b
    a = b
    b = next
} }

fun main() {
val n = 10 // Change this value to print more or fewer numbers
printFibonacci(n)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Factorial Calculation

Write a recursive function to calculate the factorial of a given number in Kotlin.

A

fun factorial(num: Int): Int {
var result = 1
for (i in 1..num) {
result *= i
}
return result
}

fun main() {
val number = 5
println(“Factorial of $number is: ${factorial(number)}”)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Palindrome Checker

Write a function that checks if a given string is a palindrome.

A

fun isPalindrome(str: String): Boolean {
val cleanedStr = str.replace(“\s”.toRegex(), “”).lowercase()
return cleanedStr == cleanedStr.reversed()
}

fun main() {
val input = “A man a plan a canal Panama”
if (isPalindrome(input)) {
println(“"$input" is a palindrome.”)
} else {
println(“"$input" is not a palindrome.”)
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Maximum of Three Numbers
Task:Write a function calledmaxOfThreethat takes three integers as parameters and returns the largest one.

A

fun maxOfThree(a: Int, b: Int, c: Int): Int {
return if (a >= b && a >= c) {
a
} else if (b >= a && b >= c) {
b
} else {
c
}
}

fun main() {
val num1 = 10
val num2 = 20
val num3 = 15

println("The largest number is: ${maxOfThree(num1, num2, num3)}") }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Write a programme which calculates the volume, area and perimeter of a given shape. Hint divide the programme into functions for specific computations.

A

import kotlin.math.PI

// Function to calculate the area of a rectangle
fun rectangleArea(length: Double, width: Double): Double {
return length * width
}

// Function to calculate the perimeter of a rectangle
fun rectanglePerimeter(length: Double, width: Double): Double {
return 2 * (length + width)
}

// Function to calculate the volume of a cuboid
fun cuboidVolume(length: Double, width: Double, height: Double): Double {
return length * width * height
}

// Function to calculate the area of a circle
fun circleArea(radius: Double): Double {
return PI * radius * radius
}

// Function to calculate the perimeter (circumference) of a circle
fun circlePerimeter(radius: Double): Double {
return 2 * PI * radius
}

// Main function to demonstrate the calculations
fun main() {
// Example: Calculations for a rectangle
val length = 5.0
val width = 3.0
val height = 4.0
val radius = 2.5

println("Rectangle:")
println("Area: ${rectangleArea(length, width)}")
println("Perimeter: ${rectanglePerimeter(length, width)}")

println("\nCuboid:")
println("Volume: ${cuboidVolume(length, width, height)}")

println("\nCircle:")
println("Area: ${circleArea(radius)}")
println("Perimeter (Circumference): ${circlePerimeter(radius)}") }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly