0% found this document useful (0 votes)
5 views30 pages

R and Kotlin Programming Examples

The document contains a series of programming exercises in R and Kotlin, showcasing various fundamental programming concepts such as creating a simple calculator, generating Fibonacci numbers, calculating factorials, and working with data structures like arrays and data frames. Each exercise includes sample code and expected output, providing a practical approach to learning programming. The exercises cover a range of topics including mathematical operations, data visualization, and object-oriented programming.

Uploaded by

balotra2004
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views30 pages

R and Kotlin Programming Examples

The document contains a series of programming exercises in R and Kotlin, showcasing various fundamental programming concepts such as creating a simple calculator, generating Fibonacci numbers, calculating factorials, and working with data structures like arrays and data frames. Each exercise includes sample code and expected output, providing a practical approach to learning programming. The exercises cover a range of topics including mathematical operations, data visualization, and object-oriented programming.

Uploaded by

balotra2004
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

3IT04 Advanced Programming Practices

R Programs

1. Write a R program to make a Simple Calculator. Take input from the user and display the
result.

• Code:

num1 <- [Link](readline("Enter first number: "))


num2 <- [Link](readline("Enter second number: "))
op <- readline("Enter operator (+, -, *, /): ")

if(op == "+") {
result <- num1 + num2
} else if(op == "-") {
result <- num1 - num2
} else if(op == "*") {
result <- num1 * num2
} else if(op == "/") {
result <- num1 / num2
} else {
result <- "Invalid operator"
}

cat("Result:", result)

• Output:

Enter first number: 10


Enter second number: 5
Enter operator (+, -, *, /): *
Result: 50

24IT602 1
3IT04 Advanced Programming Practices

2. Write a R program to get the first 5 Fibonacci numbers.

• Code:

fib <- numeric(5)


fib[1] <- 0
fib[2] <- 1
for(i in 3:5) {
fib[i] <- fib[i-1] + fib[i-2]
}
print(fib)

• Output:

[1] 0 1 1 2 3

24IT602 2
3IT04 Advanced Programming Practices

3. Write a R program to Find the Factorial of a Number Using Recursion

• Code:

fact <- function(n) {


if(n == 0)
return(1)
else
return(n * fact(n - 1))
}

num <- [Link](readline("Enter a number: "))


result <- fact(num)
cat("Factorial of", num, "is", result)

• Output:

Enter a number: 5
Factorial of 5 is 120

24IT602 3
3IT04 Advanced Programming Practices

4. Write a R program to get all prime numbers up to a given number.

• Code:

num <- [Link](readline("Enter a number: "))


for(i in 2:num) {
flag <- 0
for(j in 2:(i-1)) {
if(i %% j == 0) {
flag <- 1
break
}
}
if(flag == 0)
cat(i, " ")
}

• Output:

Enter a number: 20
2 3 5 7 11 13 17 19

24IT602 4
3IT04 Advanced Programming Practices

5. Write a R program to find the maximum and the minimum value of a given vector (values:
5,10,20,23,39)

• Code:

v <- c(5, 10, 20, 23, 39)


cat("Maximum value:", max(v), "\n")
cat("Minimum value:", min(v))

• Output:

Maximum value: 39
Minimum value: 5

24IT602 5
3IT04 Advanced Programming Practices

6. Write a R program to create a simple bar plot of your last semester subjects marks.

• Code:

subjects <- c("Maths", "DBMS", "OS", "CN", "DAA")


marks <- c(85, 78, 90, 88, 82)
barplot(marks, [Link] = subjects, col = "skyblue", main = "Last Semester Marks", xlab =
"Subjects", ylab = "Marks")

• Output:

24IT602 6
3IT04 Advanced Programming Practices

7. Write a R program to find sum of natural numbers

• Code:

n <- [Link](readline("Enter a number: "))


sum_n <- n * (n + 1) / 2
cat("Sum of natural numbers up to", n, "is", sum_n)

• Output:

Enter a number: 10
Sum of natural numbers up to 10 is 55

24IT602 7
3IT04 Advanced Programming Practices

8. Write a R program to print the multiplication table of a number

• Code:

n <- [Link](readline("Enter a number: "))


for(i in 1:10) {
cat(n, "x", i, "=", n * i, "\n")
}

• Output:

Enter a number: 5
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

24IT602 8
3IT04 Advanced Programming Practices

9. Write a R program to create a Data Frames which contain details of 3 Students and display
summary of the data. (Name , ID , Branch , Semester)

• Code:

Name <- c("Om", "Yash", "Parth")


ID <- c(101, 102, 103)
Branch <- c("CSE", "IT", "ECE")
Semester <- c(6, 6, 6)

students <- [Link](Name, ID, Branch, Semester)


print(students)
summary(students)

• Output:

Name ID Branch Semester


1 Om 101 CSE 6
2 Yash 102 IT 6
3 Parth 103 ECE 6

24IT602 9
3IT04 Advanced Programming Practices

10. Write a R program to create an array of two 3x3 matrices each with 3 rows and 3 columns
from two vectors.
(Vector 1: 1,2,3,4 Vector 2:20,21,22,23,24,25)

• Code:

v1 <- c(1, 2, 3, 4)
v2 <- c(20, 21, 22, 23, 24, 25)
arr <- array(c(v1, v2), dim = c(3, 3, 2))
print(arr)

• Output:

,,1

[,1] [,2] [,3]


[1,] 1 4 21
[2,] 2 20 22
[3,] 3 23 24

,,2

[,1] [,2] [,3]


[1,] 25 1 4
[2,] 2 3 20
[3,] 21 22 23

24IT602 10
3IT04 Advanced Programming Practices

Kotlin Programs

11. Write a Kotlin Program to find Factorial of a given number

• Code:

fun main() {
print("Enter a number: ")
val num = readLine()!!.toInt()
var fact = 1
var i = 1
while (i <= num) {
fact *= i
i++
}
println("Factorial of $num is $fact")
}

• Output:

Enter a number: 5
Factorial of 5 is 120

24IT602 11
3IT04 Advanced Programming Practices

12. Write a Kotlin Program to create simple Calculator using “when”

• Code:

fun main() {
print("Enter first number: ")
val num1 = readLine()!!.toDouble()
print("Enter second number: ")
val num2 = readLine()!!.toDouble()
print("Enter operator (+, -, *, /): ")
val op = readLine()

val result = when(op) {


"+" -> num1 + num2
"-" -> num1 - num2
"*" -> num1 * num2
"/" -> num1 / num2
else -> "Invalid operator"
}

println("Result: $result")
}

• Output:

Enter first number: 10


Enter second number: 5
Enter operator (+, -, *, /): *
Result: 50.0

24IT602 12
3IT04 Advanced Programming Practices

13. Write a Kotlin Program to find GCD and LCM of given two Numbers

• Code:

fun main() {
print("Enter first number: ")
val num1 = readLine()!!.toInt()
print("Enter second number: ")
val num2 = readLine()!!.toInt()

var a = num1
var b = num2

while (b != 0) {
val temp = b
b=a%b
a = temp
}

val gcd = a
val lcm = (num1 * num2) / gcd

println("GCD of $num1 and $num2 is $gcd")


println("LCM of $num1 and $num2 is $lcm")
}

• Output:

Enter first number: 12


Enter second number: 18
GCD of 12 and 18 is 6
LCM of 12 and 18 is 36

24IT602 13
3IT04 Advanced Programming Practices

14. Write a Kotlin Program to check Whether a Number is Palindrome or Not

• Code:

fun main() {
print("Enter a number: ")
val num = readLine()!!.toInt()
var original = num
var reversed = 0

while (original != 0) {
val digit = original % 10
reversed = reversed * 10 + digit
original /= 10
}

if (num == reversed)
println("$num is a Palindrome number")
else
println("$num is not a Palindrome number")
}

• Output:

Enter a number: 121


121 is a Palindrome number

24IT602 14
3IT04 Advanced Programming Practices

15. Write a Kotlin Program to find all Roots of a Quadratic Equation

• Code:

import [Link]

fun main() {
print("Enter coefficient a: ")
val a = readLine()!!.toDouble()
print("Enter coefficient b: ")
val b = readLine()!!.toDouble()
print("Enter coefficient c: ")
val c = readLine()!!.toDouble()

val d = b * b - 4 * a * c

if (d > 0) {
val root1 = (-b + sqrt(d)) / (2 * a)
val root2 = (-b - sqrt(d)) / (2 * a)
println("Roots are real and distinct:")
println("Root 1 = $root1")
println("Root 2 = $root2")
} else if (d == 0.0) {
val root = -b / (2 * a)
println("Roots are real and equal:")
println("Root 1 = Root 2 = $root")
} else {
val realPart = -b / (2 * a)
val imagPart = sqrt(-d) / (2 * a)
println("Roots are complex and imaginary:")
println("Root 1 = $realPart + ${imagPart}i")
println("Root 2 = $realPart - ${imagPart}i")
}
}

• Output:

Enter coefficient a: 1
Enter coefficient b: -3
Enter coefficient c: 2
Roots are real and distinct:
Root 1 = 2.0
Root 2 = 1.0

24IT602 15
3IT04 Advanced Programming Practices

16. Write a Kotlin Program to print Armstrong Numbers between Intervals Using Function

• Code:

fun isArmstrong(num: Int): Boolean {


var n = num
var sum = 0
val digits = [Link]().length

while (n != 0) {
val digit = n % 10
sum += [Link]([Link](), [Link]()).toInt()
n /= 10
}
return sum == num
}

fun main() {
print("Enter start of interval: ")
val start = readLine()!!.toInt()
print("Enter end of interval: ")
val end = readLine()!!.toInt()

println("Armstrong numbers between $start and $end are:")


for (i in start..end) {
if (isArmstrong(i)) {
print("$i ")
}
}
}

• Output:

Enter start of interval: 100


Enter end of interval: 500
Armstrong numbers between 100 and 500 are:
153 370 371 407

24IT602 16
3IT04 Advanced Programming Practices

17. Write a Kotlin Program to print Perfect Numbers between Intervals Using Function

• Code:

fun isPerfect(num: Int): Boolean {


var sum = 0
for (i in 1 until num) {
if (num % i == 0)
sum += i
}
return sum == num
}

fun main() {
print("Enter start of interval: ")
val start = readLine()!!.toInt()
print("Enter end of interval: ")
val end = readLine()!!.toInt()

println("Perfect numbers between $start and $end are:")


for (i in start..end) {
if (isPerfect(i)) {
print("$i ")
}
}
}

• Output:

Enter start of interval: 1


Enter end of interval: 1000
Perfect numbers between 1 and 1000 are:
6 28 496

24IT602 17
3IT04 Advanced Programming Practices

18. Write a Kotlin Program to multiply two Matrices by Passing Matrix to a Function

• Code:

fun multiplyMatrices(a: Array<IntArray>, b: Array<IntArray>, r1: Int, c1: Int, c2: Int):
Array<IntArray> {
val result = Array(r1) { IntArray(c2) }
for (i in 0 until r1) {
for (j in 0 until c2) {
for (k in 0 until c1) {
result[i][j] += a[i][k] * b[k][j]
}
}
}
return result
}

fun main() {
val a = arrayOf(
intArrayOf(1, 2, 3),
intArrayOf(4, 5, 6)
)

val b = arrayOf(
intArrayOf(7, 8),
intArrayOf(9, 10),
intArrayOf(11, 12)
)

val result = multiplyMatrices(a, b, 2, 3, 2)

println("Resultant Matrix:")
for (row in result) {
for (value in row) {
print("$value ")
}
println()
}
}

• Output:

Resultant Matrix:
58 64
139 154

24IT602 18
3IT04 Advanced Programming Practices

19. Write a Kotlin Program to add two Complex Numbers by Passing Class to a Function

• Code:

class Complex(val real: Double, val imag: Double)

fun addComplex(c1: Complex, c2: Complex): Complex {


return Complex([Link] + [Link], [Link] + [Link])
}

fun main() {
val c1 = Complex(3.5, 2.5)
val c2 = Complex(4.5, 3.5)
val result = addComplex(c1, c2)

println("Sum = ${[Link]} + ${[Link]}i")


}

• Output:

Sum = 8.0 + 6.0i

24IT602 19
3IT04 Advanced Programming Practices

20. Write a Kotlin Program to add two Dates

• Code:

import [Link]
import [Link]
import [Link]

fun main() {
val formatter = [Link]("dd-MM-yyyy")

print("Enter first date (dd-MM-yyyy): ")


val date1 = [Link](readLine(), formatter)

print("Enter second date (dd-MM-yyyy): ")


val date2 = [Link](readLine(), formatter)

val totalDays = [Link](date1, date2).days


val totalMonths = [Link](date1, date2).months
val totalYears = [Link](date1, date2).years

println("Difference between dates:")


println("$totalYears years, $totalMonths months, $totalDays days")
}

• Output:

Enter first date (dd-MM-yyyy): 10-03-2024


Enter second date (dd-MM-yyyy): 15-06-2025
Difference between dates:
1 years, 3 months, 5 days

24IT602 20
3IT04 Advanced Programming Practices

Julia Programs
21. Write a Julia Program to take input from the user and display the values

• Code:

name = readline()
age = parse(Int, readline())
println("Name: ", name)
println("Age: ", age)

• Output:

Om
19
Name: Om
Age: 19

24IT602 21
3IT04 Advanced Programming Practices

22. Write a Julia program to Find the Factorial of a Number Using Recursion

• Code:

function factorial(n)
if n == 0
return 1
else
return n * factorial(n-1)
end
end

println(factorial(5))

• Output:

120

24IT602 22
3IT04 Advanced Programming Practices

23. Write a Julia program to create a simple bar plot of your last semester subjects marks

• Code:

using Plots

subjects = ["Math", "CS", "Physics", "Chemistry", "English"]


marks = [85, 90, 78, 88, 92]

bar(subjects, marks, title="Last Semester Marks", xlabel="Subjects", ylabel="Marks")

• Output:

24IT602 23
3IT04 Advanced Programming Practices

24. Write a Julia program to find sum of natural numbers.

• Code:

n = 10
sum_n = 0
for i in 1:n
sum_n = sum_n + i
end
println(sum_n)

• Output:

55

24IT602 24
3IT04 Advanced Programming Practices

25. Write a Julia program for String Manipulations.

• Code:

s = "Julia"
# Uppercase (manual for small demo, ASCII logic)
upper_s = ""
for c in s
if c >= 'a' && c <= 'z'
upper_s *= Char(Int(c) - 32)
else
upper_s *= c
end
end
println(upper_s)

# Lowercase
lower_s = ""
for c in s
if c >= 'A' && c <= 'Z'
lower_s *= Char(Int(c) + 32)
else
lower_s *= c
end
end
println(lower_s)

# Length
len = 0
for c in s
len += 1
end
println(len)

# Replace "Julia" with "Python"


s2 = ""
if s == "Julia"
s2 = "Python"
else
s2 = s
end
println(s2)

24IT602 25
3IT04 Advanced Programming Practices

# Reverse
rev_s = ""
for i in length(s):-1:1
rev_s *= s[i]
end
println(rev_s)

• Output:

JULIA
julia
5
Python
ailuJ

24IT602 26
3IT04 Advanced Programming Practices

26. Write a Julia program to print the multiplication table of a number

• Code:

num = 5
for i in 1:10
println("$(num) x $(i) = ", num*i)
end

• Output:

5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

24IT602 27
3IT04 Advanced Programming Practices

27. Write a Julia program to illustrate the use of acos() method

• Code:

x = 0.5
println(acos(x))

• Output:

1.0471975511965979

24IT602 28
3IT04 Advanced Programming Practices

28. Write a Julia program to illustrate the use of count() method

• Code:

arr = [1, 2, 3, 4, 2, 2, 5, 3]

println(count(==(2), arr))

println(count(x -> x > 3, arr))

• Output:

3
2

24IT602 29
3IT04 Advanced Programming Practices

29. Write a Julia program to create an array of two 3x3 matrices each with 3 rows and 3 columns
from two vectors. (Vector 1: 1,2,3,4 Vector 2:18,19.20,21,22,23)

• Code:

v1 = [1,2,3,4,5,6,7,8,9]
v2 = [18,19,20,21,22,23,24,25,26]

m1 = Array{Int}(undef,3,3)
m2 = Array{Int}(undef,3,3)

k=1
for i in 1:3
for j in 1:3
m1[i,j] = v1[k]
m2[i,j] = v2[k]
k += 1
end
end

println(m1)
println(m2)

• Output:

[1 2 3; 4 5 6; 7 8 9]
[18 19 20; 21 22 23; 24 25 26]

24IT602 30

You might also like