Swift Program to calculate the sum of first N odd numbers


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

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 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 the first N odd numbers using the following mathematical formula.

Formula

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

Sum = N * N

Below is a demonstration of the same −

Input

Suppose our given input is −

Number - 6

Output

The desired output would be −

1+3+5+7+9+11 = 36 or 6*6 = 36
Sum of first 6 odd numbers is 36

Algorithm

Following is the algorithm −

  • Step 1 − Create a function.

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

let sum = a * a
  • Step 3 − Return the sum

  • Step 4 − Declare a variable named “num”. Here the value of “num” can be user 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 find the sum of first N odd numbers.

import Foundation import Glibc // Function to calculate the sum of first N Odd numbers func sumOddNum(a : Int) -> Int{ let sum = a * a return sum } var num = 12 // Calling function and displaying output print("Sum of first \(num) odd numbers: ", sumOddNum(a:num))

Output

Sum of first 12 odd numbers: 144

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

sum = a * a

So the working of the above code is −

sumEvenNum(12): sum = 12 * 12 = 144

Or we can say the sum of first 12 odd numbers is

1+3+5+7+9+11+13+15+17+19+21+23 = 144

Example 2

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

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

STDIN Input

Please enter the number(N): 15

Output

Sum of first 15 odd numbers: 225

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

sum = a * a

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

So the working of the above code is:

Input = 15 
sumEvenNum(15): sum = 15 * 15 = 225

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

1+3+5+7+9+11+13+15+17+19+21+23+25+27+29 =225

Updated on: 25-Aug-2022

376 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements