0

This is the code prompting the user for input

#include <stdio.h>
#include <cs50.h>

//prompt user for number

int main(void)
{
      long number = get_long("Enter card number: "); 
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
mhashir23
  • 23
  • 1
  • 2

4 Answers4

2

The below program will help you, but its bettor to give some effort

     #include <stdio.h>
     #include <cs50.h>

        //prompt user for numb er

    int main(void)
    {
        long number = get_long("Enter card number: "); 
        int count = 0; 
        do{ 
            number = number / 10; 
            ++count; 
        }while (number != 0) ;

        printf("length of number = %d",count);

    }
SayAz
  • 751
  • 1
  • 9
  • 16
1

You can do for instance:

#include <stdio.h>
#include <string.h>
void main()
{
    long alpha = 352;
    char s[256];
    sprintf(s,"%ld",(alpha >= 0L) ? alpha : -alpha );
    printf("long digits: %lu",strlen(s));

}
Jean-Marc Volle
  • 3,113
  • 1
  • 16
  • 20
1

If you allow yourself to use floating point, the number of digits of a positive integer number A in base B is the logarithm of A in base B plus 1. Working in base 10 you could write:

long get_num_digits(long a)
{
    long c;

    if (a == 0) {
        return 1;
    }
    if (a < 0) {
        a = -a;
    }
    c = (long)log10((double)a);
    ++c;

    return c;
}
  • 1
    I'm not convinced this will be accurate for all values that can be represented in a `long`. If both `long` and `double` have 64 bits, such as on almost all non-Windows 64-bit platforms, a `long` has more precision than a `double` does, so a large `long` value will "lose" its trailing **bits** when converted to a `double`. That means the `double` will represent a **smaller** absolute value than the original `long`. When that smaller value is converted to decimal digits, it can therefore have less digits than the initial `long` value would have had. – Andrew Henle Jun 06 '20 at 17:56
  • Even though the cast to double looses precision, any long can be cast to double without loosing the order of magnitude. Since the objective is to find the number of digits, the precision is not important and the result will be indeed correct to any integer long input. – Marcelo Roberto Jimenez Jun 06 '20 at 20:53
  • 1
    The `double` value retains the order of magnitude in **binary**. That doesn't mean it retains the same order of magnitude in decimal. For example: 1003 is `1111101011` in binary. Chop off the lower four bits to `1111100000` binary and the result is 992. The **binary** order of magnitude didn't change, but the decimal one sure did. – Andrew Henle Jun 06 '20 at 22:10
  • Definitely an interesting point you raise here. The problem should be seen in a transition from a 999...999 number to 1000...000. But since a power of ten is also a multiple of two, the digital representation of the power of ten will have at least a number of zeroes to the right equal to the order of magnitude in base 10. So you would need to chop a number of bits at least equal to the order of magnitude plus one to manifest the problem. – Marcelo Roberto Jimenez Jun 07 '20 at 00:31
  • That will not happen when converting 64 bits integers to IEEE doubles because the last has 53 significant bits, the order of magnitude of the maximum 64 bit integer is 10^18 (19 digits), so we would chop 64 - 53 = 11 bits, which is less than 18. In order to force the problem to manifest itself, we can use float instead of double. IEEE floats have 24 significant bits. If we use 10^11, which is a 37 bit number, 37 - 24 = 13, which is more than 11. Indeed, I have written a simple program to test it, and in fact, if you use floats instead of doubles you will have a problem. – Marcelo Roberto Jimenez Jun 07 '20 at 00:31
  • Thank you for drawing the attention to that issue. – Marcelo Roberto Jimenez Jun 07 '20 at 00:31
0

This version addresses an issue raised by Andrew Henle and is capable of dealing with a long with an arbitrary number of bits.

After the necessary precautions treating non-positive arguments, we calculate the log of the argument in base 2 and change the log base to base 10 by multiplying by lod_10(2).

The number of digits from the base 10 log of 2 must be chosen accordingly. The following version is good from 1 to 1000 bits longs, uses no floating point, one integer multiplication, one interger division and a maximum of N-1 shifts, where N is the number of bits of the argument.


    /*
     * Using 3 digits from log_10(2), we have enough precision to calculate the 
     * number of digits of a 1000 bit number.
     */
    /* #define LOG_10_2 301029995663981198 */
    #define LOG_10_2_NUMERATOR    301
    #define LOG_10_2_DENOMINATOR 1000

    long get_num_digits3(long a)
    {
        long c;
        long i;

        if (a == 0) {
            return 1;
        }
        if (a < 0) {
            a = -a;
        }
        /* i = log_2(a); */
        i = 0;
        while (a) {
        a >>= 1;    
        ++i;
        }
        /* c = log_10(a) + 1 */
        c = (long)(LOG_10_2_NUMERATOR * i);
        c /= LOG_10_2_DENOMINATOR;
        ++c;

        return c;
    }