## Basic Algorithm 4: Length of an Integer

Posted by Ed on November 22, 2011

Last time we talked about cutting a number. Today we have the following problem:

Write a function that takes an integer and returns the length of that integer, i.e. the number of digits.

Example: 2011 has length 4.

Test your function with the following integers:

a) 31415926

b) 1000

c) 5

d) 0

Bonus: Write a non-recursive and a recursive function.

**Here is my solution in C++:**

#include <iostream> using namespace std; // function prototypes int getLength(int x); int getLengthRecAux(int x, int length); int getLengthRec(int x); // main function -------------------------------- int main(){ int myNumber=31415926; int thousand=1000; int five=5; int zero=0; cout << "Non-recursive:" << endl; cout << myNumber << " has length: " << getLength(myNumber) << endl; cout << thousand << " has length: " << getLength(thousand) << endl; cout << five << " has length: " << getLength(five) << endl; cout << zero << " has length: " << getLength(zero) << endl; cout << "\nRecursive:" << endl; cout << myNumber << " has length: " << getLengthRec(myNumber) << endl; cout << thousand << " has length: " << getLengthRec(thousand) << endl; cout << five << " has length: " << getLengthRec(five) << endl; cout << zero << " has length: " << getLengthRec(zero) << endl; cout << "\nEnd of program" << endl; } // ---------------------------------------------- // Non-recursive int getLength(int x){ if(x==0){ return 1; } int length=0; while(x>0){ length++; x=x/10; } return length; } // Recursive ------------------------------------- // getLengthRecAux is the actual recursive function // which serves as an auxiliary function // for getLengthRec() int getLengthRecAux(int x, int length){ if(x==0){ return length; } // Note that I don't use an else block here. // Reason: suppose the if block is fulfilled then the // return statement will stop the recursion and we will "leave" the function return getLengthRecAux(x/10, ++length); // Note that length++ does not work, since length is passed to the function and then increased by 1. // So, we should use ++length, i.e. increase by 1 and then pass the value. // Alternatively, we could use length=length+1. } // getLengthRec() contains the auxiliary function getLengthRecAux() // this allows to handle the case x==0 int getLengthRec(int x){ if(x==0){ return 1; } return getLengthRecAux(x, 0); }

**Here is my solution in Python 3:**

# Non-recursive ------------------------------------------- def getLength(x): if(x==0): return 1 length=0 while(x>0): length=length+1 x=x//10 # Don't forget the double slash in Python 3 return length # Recursive ---------------------------------------------- # getLengthRecAux is the actual recursive function # which serves as an auxiliary function # for getLengthRec() def getLengthRecAux(x, length): if(x==0): return length # Note that I don't use an else block here. # Reason: suppose the if block is fulfilled then the # return statement will stop the recursion and we will "leave" the function return getLengthRecAux(x//10, length+1) def getLengthRec(x): if(x==0): return 1 return getLengthRecAux(x,0) #------------------------------------------------------ # main function def main(): myNumber=31415926; thousand=1000; five=5; zero=0; print("Non-recursive:") print(myNumber, "has length: ", getLength(myNumber)) print(thousand, "has length: ", getLength(thousand)) print(five, "has length: ", getLength(five)) print(zero, "has length: ", getLength(zero)) print("\nRecursive:") print(myNumber, "has length: ", getLengthRec(myNumber)) print(thousand, "has length: ", getLengthRec(thousand)) print(five, "has length: ", getLengthRec(five)) print(zero, "has length: ", getLengthRec(zero)) print("\nEnd of program") #------------------------------------------------------- main()

**Here is my solution in Java:**

public class BasicAlgorithm4_lengthInteger { // Non-recursive -------------------------------- int getLength(int x){ if(x==0){ return 1; } int length=0; while(x>0){ x=x/10; ++length; } return length; } // Recursive ------------------------------------- // getLengthRecAux is the actual recursive function // which serves as an auxiliary function // for getLengthRec() int getLengthRecAux(int x, int length){ if(x==0){ return length; } // Note that I don't use an else block here. // Reason: suppose the if block is fulfilled then the // return statement will stop the recursion and we will "leave" the function return getLengthRecAux(x/10, ++length); // Note that length++ does not work, since length is passed to the function and then increased by 1. // So, we should use ++length, i.e. increase by 1 and then pass the value. // Alternatively, we could use length=length+1. } // getLengthRec() contains the auxiliary function getLengthRecAux() // this allows to handle the case x==0 int getLengthRec(int x){ if(x==0){ return 1; } return getLengthRecAux(x, 0); } // main function ------------------------------------------------------------------- public static void main(String[] args){ int myNumber=31415926; int thousand=1000; int five=5; int zero=0; BasicAlgorithm4_lengthInteger myObj = new BasicAlgorithm4_lengthInteger(); System.out.println("Non-recursive:"); System.out.println(myNumber + " has length: " + myObj.getLength(myNumber)); System.out.println(thousand + " has length: " + myObj.getLength(thousand)); System.out.println(five + " has length: " + myObj.getLength(five)); System.out.println(zero + " has length: " + myObj.getLength(zero)); System.out.println("\nRecursive:"); System.out.println(myNumber + " has length: " + myObj.getLengthRec(myNumber)); System.out.println(thousand + " has length: " + myObj.getLengthRec(thousand)); System.out.println(five + " has length: " + myObj.getLengthRec(five)); System.out.println(zero + " has length: " + myObj.getLengthRec(zero)); System.out.println("\nEnd of program"); } }

Advertisements

## Increment operator: difference between ++i and i++ « Learntofish's Blog said

[…] Basic Algorithm 4: Length of an Integer […]

## Basic Algorithm 4b: Length of an integer (part 2) « Learntofish's Blog said

[…] Basic Algorithm 4: Length of an Integer […]