0% found this document useful (0 votes)
10 views2 pages

Scala 1

Uploaded by

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

Scala 1

Uploaded by

a90685766
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

object CombinedScalaProgram {

def main(args: Array[String]): Unit = {

val sc = new java.util.Scanner(System.in)

// Question 1

println("Enter a number:"); val num = sc.nextInt()

println(if (num > 0) "Positive" else if (num < 0) "Negative" else "Zero")

// Question 2

println("Enter four numbers:"); val secondMax = Array.fill(4)(sc.nextInt()).sorted.apply(2)

println(s"Second Maximum: $secondMax")

// Question 3

println("Enter a number for factorial:"); val n = sc.nextInt()

println(s"Factorial: ${(1 to n).product}")

// Question 4

println("Enter range n1 and n2:"); val (n1, n2) = (sc.nextInt(), sc.nextInt())

val avg = (n1 to n2).sum.toDouble / (n2 - n1 + 1)

println(s"Average: $avg")

}
object CombinedScalaProgram {

def main(args: Array[String]): Unit = {

val sc = new java.util.Scanner(System.in)

// Question 1: Check Perfect Numbers

println("Enter 5 numbers:")

for (_ <- 1 to 5) {

val n = sc.nextInt()

val sumOfDivisors = (1 until n).filter(n % _ == 0).sum

println(s"$n is ${if (sumOfDivisors == n) "Perfect" else "Not Perfect"}")

// Question 2: Sum of Primes Between 1 and 100

val primeSum = (2 to 100).filter(n => (2 until n).forall(n % _ != 0)).sum

println(s"Sum of primes between 1 and 100: $primeSum")

// Question 3: Convert Integer to Binary and Octal

println("Enter a number for binary and octal conversion:")

val num = sc.nextInt()

println(s"Binary: ${Integer.toBinaryString(num)}, Octal: ${Integer.toOctalString(num)}")

// Question 4: Fibonacci Series

println("Enter the limit for the Fibonacci series:")

val limit = sc.nextInt()

var (a, b) = (0, 1)

print("Fibonacci series: ")

while (a <= limit) {

print(s"$a ")

val next = a + b

a=b

b = next

println()

You might also like