1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: In need of a formula

  1. Oct 28, 2004 #1
    Okay, if I was given the number 4346, there are 4 digits there right? duh! :tongue2:

    If I was given 45645, there are 5 digits right? duh! :tongue2:

    Okay, does anyone know a formula to calculate the number of digits in a number?
  2. jcsd
  3. Oct 28, 2004 #2
    Yeah, divide the number by 10, 100, 1000, 10000, and so on until you get a result that is less than 10 and more than 1, then the number has the same number of digits with the number you last divided it by.

    For example,

    45654/10 > 10, so no.
    45654/100 > 10, again no.
    45654/1000 > 10, nope.
    45654/10000 is < 10, so yes!

    10000 is a 5 digit number so 45654 is a 5 digit number.

    I hope you don't take me too seriously because I don't know any special formulae, but this is how I think of the problem anyway.
  4. Oct 28, 2004 #3
    That's exactly what I did for my Computer Science program. We were told to switch any given number so it would read backwards...ex...5432 --> 2345

    Not knowing a forumla, what you described above is exactly what I did
  5. Oct 28, 2004 #4
    Switching numbers to read backwards is fun. I've never taken a single computer lesson, but I have a sketchy image of how programming works. How did you do the problem though? I don't see how you could have arrived at the solution with the method I mentioned earlier.

    Lets see:

    5432 is a 4-digit number so that means that the "highest number" able to divide it is 1000.

    5432/1000 = 5.432

    I think there is an in-built function in most computer programmes that allow you to take the integer part of a number, is there not?

    So we take the integer part of the number, which is 5 and call it z.

    We minus 1000z from 5432 to get 432, which is a three-digit number and is 4.32 when divided by 100. So we can obtain 4 with the integer function and call it y.

    I think this can continue until we have x (3) and w (2) as well.

    So we can string them together to get 1000w + 100x + 10y + z = 2345

    Is that how you did it? Is there a shorter algorithm for it?
  6. Oct 28, 2004 #5


    User Avatar
    Science Advisor
    Gold Member

    I don't know what language you're using, but I'd convert it to a string and work with it that way. In Visual Basic:
    Code (Text):

    n = 12345
    s = str$(n)
    r = ""
    for k=len(s) to 1  step -1
    next k
    n2 = val(r)
  7. Oct 29, 2004 #6
    Is there a good site online that teaches Visual Basic? I don't really know any programming but I would like to start with Visual Basic. I also need a compiler for writing programs in VBasic. Is there anywhere I can download it for free?
  8. Oct 29, 2004 #7


    User Avatar

    I recommend taking this question over to the computer science forum on this website. They have been very helpful with my Java problems.
  9. Oct 29, 2004 #8
    From a mathematical point of view if a number N contains n digits, we can write it as

    [tex]N = a_{0}a_{1}a_{2}a_{3}....a_{n-1}[/tex]

    In decimal notation,

    [tex]N = \sum_{i = 0}^{n-1}a_{i}10^i[/tex]

    Your problem is to convert N to N' which is

    [tex]N' = a_{n-1}....a_{3}a_{2}a_{1}a_{0} [/tex]


    [tex]N' = \sum_{i = 0}^{n-1} a_{i}10^{n-i-1}[/tex]

    The digits can be extracted using division by 10.

    Last edited: Oct 29, 2004
  10. Oct 29, 2004 #9
    I program in the language Java, all I did was the following:

    public class program12 {
    public static void main(String[] args) {

    int number=0, counter=0,trueOrFalse=0;
    String firstNum;

    firstNum=JOptionPane.showInputDialog("Enter integer: ");
    trueOrFalse= factorial(number);

    public static int factorial (int integer)
    int tens=1,mod=0,value=0,temp=0, counterTens=10,counterOnes=1;

    while (mod!=integer){

    for (int tenSec=10;tenSec<=tens;tenSec=tenSec*10){
    return (temp);

    Basically...enter 5321

    It counts that it has 4 digits
    then does 1000+200+30+5 = 1235
    Last edited: Oct 29, 2004
  11. Oct 29, 2004 #10


    User Avatar
    Science Advisor
    Homework Helper

    If n>0 is an integer, then it has [tex]floor(\log_{10}n+1)[/tex] digits. I don't know if this will be preferable to your loop though.
  12. Oct 30, 2004 #11
    I'll never ever learn how to program. :(

    My chances of mastering Latin should be higher than mastering any programming languages.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook