Swift Program to calculate the sum of all even numbers up to N


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

A number that is a 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 all the even numbers upto N by adding all the even numbers present in the given list.

Below is a demonstration of the same −

Input

Suppose our given input is −

Number 10

Output

The desired output would be −

2+4+6+8+10 = 30.
Sum of even numbers is 30

Algorithm

Following is the algorithm −

  • Step 1 − Create a function.

  • Step 2 − Declare a variable to store the sum of the even numbers upto N - sum = 0.

  • Step 3 − Run a for loop from 0 to N.

  • Step 4 − Check if the given number is an even number or not.

if j % 2 == 0

Here using % operator we find the remainder.

  • Step 5 − Calculate the sum of even numbers.

sum += j
  • Step 6 − Return the sum.

  • Step 7 − Create a variable named “num” to store the value of N. Here the value of num can be user-defined or pre-defined.

  • Step 8 − Call the function and pass “num” as an argument in it.

  • Step 9 − Print the output

Example 1

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

import Foundation import Glibc // Function to find the sum of even numbers func sumEvenNum(a : Int) -> Int{ var sum = 0 print("Even numbers from 0 to \(a):") for j in 0...a{ // Check if the specified number is even number or not if j % 2 == 0{ // Calculating the sum of even numbers sum += j print(j, terminator: " ,") } } return sum } var num = 16 // Calling the function and displaying the sum print("\nSum of all the even numbers from 0 to \(num): ", sumEvenNum(a:num))

Output

Even numbers from 0 to 16:
0 ,2 ,4 ,6 ,8 ,10 ,12 ,14 ,16 ,
Sum of all the even numbers from 0 to 16:  72

In the above code, we create a function named sumEvenNum() function to find the sum of all the even numbers upto N. This function takes one argument. So the working of the sumEvenNum() function is −

sumEvenNum(16): 
sum = 0

1st Iteration:
for j in 0...16
   if 0 % 2 == 0 // true 
      sum = sum + j = 0 + 0 = 0 
      print(j, terminator: " ,”) // print 0 
sum = 0 

2nd Iteration: 
for j in 0...16 
   if 1 % 2 == 0 // false 
      sum = 0 
      print(j, terminator: " ,”) 
sum = 0

3rd Iteration: 
for j in 0...16 
   if 2 % 2 == 0 // true 
      sum = 0 + 2 = 2 
      print(j, terminator: " ,”) // print 2 
sum = 2

4th Iteration: 
for j in 0...16 
   if 3 % 2 == 0 // false
      sum = 2 
      print(j, terminator: " ,”) 
sum = 2

5th Iteration: 
for j in 0...16 
   if 4 % 2 == 0 // true 
      sum = 2 + 4 = 6 
      print(j, terminator: " ,”) // print 4 
sum = 6 ….. 

so on till 16.

Now we display the sum of all the even numbers present from 0 to 16 which is 72(0+2+4+6+8+10+12+14+16 = 72).

Example 2

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

import Foundation import Glibc // Function to find the sum of even numbers func sumEvenNum(a : Int) -> Int{ var sum = 0 print("Even numbers from 0 to \(a):") for j in 0...a{ // Checking if the given number is even number if j % 2 == 0{ // Finding the sum of even numbers sum += j print(j, terminator: " ,") }
} return sum } print("Enter the number") var num = Int(readLine()!)! print("\nSum of all the even numbers from 0 to \(num): ", sumEvenNum(a:num))

STDIN Input

Enter the number 
10

Output

Even numbers from 0 to 10:
0 ,2 ,4 ,6 ,8 ,10 ,
Sum of all the even numbers from 0 to 10:  30

Here the working of the above code is the same as the Example 1 the only difference is here we take the value of “num” from the user using readLine() function and convert the input value into integer type using Int() function. So here user enter number 10 so the sum of all the even numbers present between 0 to 10 is 30 (0+2+4+6+8+10 = 30)

Updated on: 25-Aug-2022

976 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements