# Swift Program to Count Number of Digits in an Integer

This tutorial will discuss how to write a Swift program to count the number of digits in an integer.

Below is a demonstration of the same −

Suppose we enter the following input −

Number = 3454634


Following is the desired output −

Total count is 7


We can count the number of digits using the following methods −

• Iterative method

• Recursion method

## Iterative Method

We can calculate the total digits present in the given integer with the help of loops like if, while loop, etc. It is the easiest and cheapest method.

### Algorithm

The algorithm is explained below −

• Step 1 − Create a function.

• Step 2 − Declare two variables with values - count = 0 and num = n.

• Step 3 − Check if the given number is equal to 0. If yes then return 1.

• Step 4 − Run a While loop with condition num > 0 and count the total number of digits by dividing the number by 10 and increasing the value of count by 1.

num = num / 10

count += 1

• Step 5 − Declare a variable with value - val = 8776

• Step 6 − Call the function with one argument and display the final output.

### Example

The following program shows how to count the number of digits in an integer using iterative method.

import Foundation
import Glibc

func countNumbers(n: Int)->Int{

// Store the total count
var count = 0

// Store the number
var num = n

// Checking the number for 0
// If yes print 1
if (num == 0){
return 1
}

// Check for the positive number
while (num > 0){

// Divide the num by 10 and store
// the quotient into the num variable
num = num / 10

// If the quotient is not zero, then update the
// count by one. If the quotient is zero,
// then stop the count
count += 1
}

// Return the final count
return count
}
let val = 8776
print("The total digits present in \(val) are-", countNumbers(n: val))


### Output

The total digits present in 8776 are- 4


Here, in the above code, we create a function named as countNumbers() to count the total digits present in the given integer. In this function, we create two variables named count = 0 and num = n, where the count is used to store the total count and num stores the integer. Now we use a if statement to check if the given number is equal to zero or not. If the given number is equal to zero, then it will return 1. If the given number is not equal to zero, then the control enter in the while loop −

while (num > 0){
num = num / 10
count += 1
}


The working of the above code is −

while (8776 > 0){
num = 8776 / 10 = 877
count = 0 + 1 = 1
}
while (877 > 0){
num = 877 / 10 = 87
count = 1 + 1 = 2
}
while (87 > 0){
num = 87 / 10 = 8
count = 2 + 1 = 3
}
while (8 > 0){
num = 8 / 10 = 0
count = 3 + 1 = 4
}


After creating the function, we create a variable with value named val = 8776 and call the countNumbers() function by passing val as an argument and display the final count which is 4.

## Recursion Method

We can also count the total number of digits in the given integer using the recursion method. Recursion is a process in which a function is calling itself to solve a problem.

### Algorithm

The algorithm is explained below −

• Step 1 − Declare a variable with value- count = 0.

• Step 2 − Create a recursive function.

• Step 2 − Run if loop with condition n > 0. Increase the value of count by 1 and return the total number of digits present in the given number by calling the function itself.

• Step 5 − Declare a variable with value - val = 764434365

• Step 6 − Call the function with one argument and display the final output.

### Example

The following program shows how to count the number of digits in an integer using recursion method.

import Foundation
import Glibc

var count : Int = 0
func countNumbers(n: Int)->Int{

// Checking for positive value
if (n > 0) {

// Increase the count by one
count = count + 1

// Finding the quotient by calling the function itself
return countNumbers(n: n / 10)
}
return count
}
let val = 7644
print("The total digits present in \(val) are-", countNumbers(n: val))


### Output

The total digits present in 7644 are- 4


Here in the above code, first, we create a variable named “count” of integer type to store the total number of counts. Now we create a recursive function named countNumber(). This method counts the total number of digits by calling itself. Now we call this function with val = 7644 as an argument. So the working of this function is −

1st countNumbers(7644) function call:
if (7644 > 0) {
count = 0 + 1 = 1
return countNumbers(n: 7644 / 10)
// Returning the quotient 764
}
2nd countNumbers(764) function call:
if (764 > 0) {
count = 1 + 1 = 2
return countNumbers(n: 764 / 10)
// Returning the quotient 76
}
3rd countNumbers(76) function call:
if (76 > 0) {
count = 2 + 1 = 3
return countNumbers(n: 76 / 10)
// Returning the quotient 7
}
4th countNumbers(7) function call:
if (7 > 0) {
count = 3 + 1 = 4
return countNumbers(n: 7 / 10)
// Returning the quotient 0
}
5th countNumbers(0) function call:
// Here condition is false so the recursive call of the function is stop and return the count.
if (0 > 0)  {
count = count + 1
return countNumbers(n: n / 10)
}
return count // Returning count = 4


Display the final count which is 4.