Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
<div class="execute"></div><div class="code-mirror language-swift" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">import</span> <span class="token class-name">Foundation</span>
<span class="token keyword">import</span> <span class="token class-name">Glibc</span>
<span class="token keyword">func</span> <span class="token function-definition function">countNumbers</span><span class="token punctuation">(</span>n<span class="token punctuation">:</span> <span class="token class-name">Int</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token class-name">Int</span><span class="token punctuation">{</span>
<span class="token comment">// Store the total count </span>
<span class="token keyword">var</span> count <span class="token operator">=</span> <span class="token number">0</span>
<span class="token comment">// Store the number</span>
<span class="token keyword">var</span> num <span class="token operator">=</span> n
<span class="token comment">// Checking the number for 0</span>
<span class="token comment">// If yes print 1</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>num <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token number">1</span>
<span class="token punctuation">}</span>
<span class="token comment">// Check for the positive number </span>
<span class="token keyword">while</span> <span class="token punctuation">(</span>num <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
<span class="token comment">// Divide the num by 10 and store </span>
<span class="token comment">// the quotient into the num variable</span>
num <span class="token operator">=</span> num <span class="token operator">/</span> <span class="token number">10</span>
<span class="token comment">// If the quotient is not zero, then update the </span>
<span class="token comment">// count by one. If the quotient is zero, </span>
<span class="token comment">// then stop the count</span>
count <span class="token operator">+=</span> <span class="token number">1</span>
<span class="token punctuation">}</span>
<span class="token comment">// Return the final count </span>
<span class="token keyword">return</span> count
<span class="token punctuation">}</span>
<span class="token keyword">let</span> val <span class="token operator">=</span> <span class="token number">8776</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"The total digits present in </span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">val</span><span class="token interpolation-punctuation punctuation">)</span><span class="token string"> are-"</span></span><span class="token punctuation">,</span> <span class="token function">countNumbers</span><span class="token punctuation">(</span>n<span class="token punctuation">:</span> val<span class="token punctuation">)</span><span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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.
<div class="execute"></div><div class="code-mirror language-swift" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">import</span> <span class="token class-name">Foundation</span>
<span class="token keyword">import</span> <span class="token class-name">Glibc</span>
<span class="token keyword">var</span> count <span class="token punctuation">:</span> <span class="token class-name">Int</span> <span class="token operator">=</span> <span class="token number">0</span>
<span class="token keyword">func</span> <span class="token function-definition function">countNumbers</span><span class="token punctuation">(</span>n<span class="token punctuation">:</span> <span class="token class-name">Int</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token class-name">Int</span><span class="token punctuation">{</span>
<span class="token comment">// Checking for positive value</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>n <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// Increase the count by one</span>
count <span class="token operator">=</span> count <span class="token operator">+</span> <span class="token number">1</span>
<span class="token comment">// Finding the quotient by calling the function itself</span>
<span class="token keyword">return</span> <span class="token function">countNumbers</span><span class="token punctuation">(</span>n<span class="token punctuation">:</span> n <span class="token operator">/</span> <span class="token number">10</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token keyword">return</span> count
<span class="token punctuation">}</span>
<span class="token keyword">let</span> val <span class="token operator">=</span> <span class="token number">7644</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"The total digits present in </span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">val</span><span class="token interpolation-punctuation punctuation">)</span><span class="token string"> are-"</span></span><span class="token punctuation">,</span> <span class="token function">countNumbers</span><span class="token punctuation">(</span>n<span class="token punctuation">:</span> val<span class="token punctuation">)</span><span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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.
