Swift Program to calculate the sum of first N even numbers


This tutorial will discuss how to write a Swift program to calculate the sum of first N even numbers.

A number that is multiple of 2 or we can say that a number that is completely divisible by 2(that means leaves no remainder) is known as even number. For example, 2, 4, 6, 8, 10, …etc. are even numbers. We can calculate the sum of the first N even number using the following formula.

Formula

Following is the formula for the sum of first N even numbers −

Sum = N(N + 1)

Below is a demonstration of the same −

Input

Suppose our given input is −

Number - 6

Output

The desired output would be

2+4+6+8+10+12 = 42 or 6*(6 + 1) = 42
Sum of first 6 even numbers is 42

Algorithm

Following is the algorithm −

  • Step 1 − Create a function.

  • Step 2 − Declare a variable named “sum” to store the sum of the first N even numbers using the mathematical formula −

let sum = a * (a + 1)
  • Step 3 − Return the sum

  • Step 4 − Declare a variable named “num”. Here the value of “num” can be use r defined or pre defined.

  • Step 5 − Call the function and pass “num” as an argument to the function.

  • Step 6 − Print the output.

Example 1

The following program shows how to calculate the sum of first N even numbers.

import Foundation import Glibc // Function to calculate the sum of first N even numbers func sumEvenNum(a : Int) -> Int{ // Finding the sum of first N even numbers let sum = a * (a + 1) return sum } var num = 16 // Calling the function and displaying the output print("\nSum of first \(num) even numbers: ", sumEvenNum(a:num))

Output

Sum of first 16 even numbers: 272

In the above code, we create a function named sumEvenNum(). It takes one argument and returns the sum of first N even numbers using the following mathematical formula −

sum = a * (a + 1)

So the working of the above code is:

sumEvenNum(16): sum = 16 * (16 + 1) = 16 * 17 = 272

Or we can say the sum of the first 16 even numbers is

0+2+4+6+8+10+12+14+16+18+20+22+24+26+28+30 = 272

Example 2

The following program shows how to calculate the sum of the first N even numbers.

import Foundation import Glibc // Function to calculate the sum of first N even numbers func sumEvenNum(a : Int) -> Int{ // Finding the sum of first N even numbers let sum = a * (a + 1) return sum } // Taking input from the user print("Please enter the number:") var num = Int(readLine()!)! // Calling the function and // Displaying output print("\nSum of first \(num) even numbers: ", sumEvenNum(a:num))

STDIN Input

Please enter the number: 10

Output

Sum of first 10 even numbers: 110

In the above code, we create a function named sumEvenNum(). It takes one argument. This function is used to find the sum of first N even numbers using the following mathematical formula −

sum = a * (a + 1)

Here we take the input from the user using readLine() function and pass that input in the sumEvenNum() function.

So the working of the above code is −

Input = 10 
sumEvenNum(10): sum = 10 * (10 + 1) = 10 * 11 = 110

Or we can say the sum of the first 10 even numbers is

0+2+4+6+8+10+12+14+16+18 = 110
Or we can say the sum of the first 10 even numbers is 0+2+4+6+8+10+12+14+16+18 = 110

Updated on: 25-Aug-2022

212 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements