Scala program to add two number

1. Introduction

One of the simplest operations in any programming language is the addition of two numbers. This basic operation serves as a foundational step in understanding programming concepts. In this blog post, we will see how to add two numbers in Scala.

2. Program Steps

1. Set up the Scala environment.

2. Create an object to house the main function.

3. Declare two numbers to be added.

4. Implement a function to perform the addition.

5. Call the function within the main method and add the numbers.

6. Print the result.

7. Run the program.

3. Code Program

object AdditionApp {
  def main(args: Array[String]): Unit = {
    // Declare two numbers
    val num1 = 5
    val num2 = 7

    println(s"Number 1: $num1")
    println(s"Number 2: $num2")

    // Add the numbers using the add function
    val result = add(num1, num2)

    println(s"\nResult of Addition: $result")

  // Function to add two numbers
  def add(a: Int, b: Int): Int = {
    a + b


Number 1: 5
Number 2: 7
Result of Addition: 12


1. We start with an object named AdditionApp. In Scala, the main method, which is the program's entry point, is contained inside an object.

2. Inside the main function, we declare two numbers, num1 and num2, which we want to add.

3. These numbers are then printed to provide clarity to the user.

4. The function add houses the logic for adding two numbers. It takes two integers as parameters and returns their sum.

5. Within the main method, we call the add function to get the result of the addition and then print it.

6. As seen in the output, when executed, the program displays the two numbers and their sum.