# 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

// 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
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.6813449263572693, "perplexity": 1057.320176543817}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2023-14/segments/1679296949181.44/warc/CC-MAIN-20230330101355-20230330131355-00244.warc.gz"}