-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountDigits.cpp
More file actions
41 lines (30 loc) · 1.14 KB
/
countDigits.cpp
File metadata and controls
41 lines (30 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Description: This program calculates the total number of digits in an integer
// using a recursive approach. It manually handles negative numbers
// and zero without relying on external libraries like <cmath>.
//
// Scenario: Given an integer 'n', the program repeatedly peels off the last
// digit using integer division (n / 10) and adds 1 to the recursive
// count until a single-digit base case (n < 10) is reached.
#include <iostream>
using namespace std;
int countDigits(int n)
{
// Handle negative numbers manually without <cmath>
if (n < 0)
n = -n;
// Base case: If the number is a single digit (0-9)
if (n < 10)
return 1;
// Recursive case: Remove the last digit and add 1
return 1 + countDigits(n / 10);
}
int main()
{
int num1 = 24680;
int num2 = -1357;
int num3 = 0;
cout << "Number of digits in " << num1 << " is: " << countDigits(num1) << endl
<< "Number of digits in " << num2 << " is: " << countDigits(num2) << endl
<< "Number of digits in " << num3 << " is: " << countDigits(num3) << endl;
return 0;
}