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

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

A number that is not divisible by 2 or we can say that when an odd number is divided by 2 then it leaves some remainder such type of number is known as an odd number. For example, when 2 divides by 2 leave remainder 0 whereas 3 divides by 2 leaves remainder 1. So it means 2 is an even number and 3 is an odd number.

List of odd numbers is −

1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, ……


We can calculate the sum of all the odd numbers upto N by adding all the odd 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

1+3+5+7+9 = 25
Sum of odd numbers are 25


Algorithm

Following is the algorithm −

• Step 1 − Create a function.

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

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

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

if j % 2 != 0


Here using % operator, we find the remainder. If the remainder is not equal to 0 then the number is an odd number.

• Step 5 − Calculate the sum of odd 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 a argument in it.

• Step 9 − Print the output

Example 1

The following program shows how to find the sum of all odd numbers upto N.

import Foundation
import Glibc

// Function to calculate the sum of Odd numbers
func sumOddNum(a : Int) -> Int{
var sum = 0
print("Odd numbers from 0 to \(a):")
for j in 1...a{

// Checking if the given number is odd number or not
if j % 2 != 0{

// Calculate the sum of odd numbers
sum += j
print(j, terminator: " ,")
}
}
return sum
}
var num = 16

// Calling function and displaying output
print("\nSum of all the odd numbers from 0 to \(num): ", sumOddNum(a:num))


Output

Odd numbers from 0 to 16:
1 ,3 ,5 ,7 ,9 ,11 ,13 ,15 ,
Sum of all the odd numbers from 0 to 16:  64


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

sumOddNum(16)
sum = 0

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

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

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

4th Iteration:
for j in 0...16
if 3 % 2 != 0 // true
sum = 1 + 3 = 4
print(j, terminator: " ,”) // print 3
sum = 4
...so on till 16.

Now we display the sum of all the odd numbers present between 0 to 16 is 64(1+3+5+7+9+11+13+15 = 64).

Example 2

The following program shows how to find the sum of all odd numbers upto N.

import Foundation
import Glibc

// Function to calculate the sum of Odd numbers
func sumOddNum(a : Int) -> Int{
var sum = 0
print("Odd numbers from 0 to \(a):")
for j in 1...a{

// Checking if the given number is odd number or not
if j % 2 != 0{

// Calculate the sum of odd numbers
sum += j
print(j, terminator: " ,")
}
}
return sum
}

// Taking input from the user

// Calling function and displaying output
print("\nSum of all the odd numbers from 0 to \(num): ", sumOddNum(a:num))


STDIN Input

Please enter the number(N): 10


Output

Odd numbers from 0 to 10:
1 ,3 ,5 ,7 ,9 ,
Sum of all the odd numbers from 0 to 10:  25


Here the working of the above code is 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 odd numbers present in between 0 to 10 is 25 (1+3+5+7+9= 25)