Home > Back-end >  How works with large integers?
How works with large integers?

Time:08-17

I do not fully understand how it works with large integers. According to the code, the program receives a number from 1 * 10^15 to 1 * 10^16. So, when I write a number without 0 (for example, 3445674589632452), the code works as intended, and when I enter a number with 0 (for example, 3445674589630052), it gives me a negative number (-1048) instead 52. Please tell me what I should do.

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

int main (void)
{   
    long  number  ;
    number = get_long("Number: ");
    if (number < 0 )
    {
        do
        {
            number = get_long("Number: ");
        }
        while ( number < 0 );
    }
    if (number > 0 && number < 10000000000000)
    {
        printf("INVALID\n");
        return number; 
    }
    if (number >= 100000000000000 && number < 1000000000000000)
    {
        printf("INVALID\n");
        return number; 
    }
    if ( number >= 100000000000000000)
    {
        printf("INVALID\n");
        return number;
    }
   if (number == 0 )
   {
       printf("INVALID\n");
       return number;
   }
   if (number >= 3400000000000000 && number < 3500000000000000) 
   {             
    do
    {             
        number -=  1000000000000000;
    }
    while(number > 1000000000000000);
     do
    {
        number -=  100000000000000;
    }
    while(number > 100000000000000);
    do
    {
        number -= 10000000000000;
    }
    while(number > 10000000000000);
    do
    {
        number -= 1000000000000;
    }
    while(number > 1000000000000);
    do
    {
        number -= 100000000000;
    }
    while(number > 100000000000);
    do
    {
        number -= 10000000000;
    }
    while(number > 10000000000);
    do
    {
        number -= 1000000000;
    }
    while(number > 1000000000);
    do
    {
        number -= 100000000;
    }
    while(number > 100000000);
    do
    {
        number -= 10000000;
    }
    while(number > 10000000);
    do
    {
        number -= 1000000;
    }
    while(number > 1000000);
    do
    {
        number -= 100000;
    }
    while(number > 100000);
    do
    {
        number -= 10000;
    }
    while(number > 10000);
    do
    {
        number -= 1000;
    }
    while(number > 1000);
    do
    {
        number -= 100;
    }
    while(number >= 100); 
    printf("%li\n" ,number );
    printf("AMEX\n");
   }

CodePudding user response:

I've seen variations of this issue come across this forum, and keeping with the comments, one should utilize the acquisition of numbers as large as credit card numbers via the entry of a string value. Ultimately, you will probably need to validate the entered number via the Luhn algorithm, so keeping that in mind here is a proof of principle snippet of code that requests a credit card number entry (as a string), and then validates whether or not the entry is valid using the Luhn algorithm.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int check_valid(char * num)
{
    int sum = 0;
    int work = 0;
    char card[20];

    if ((strlen(num) %2 == 0))  /* Even numbers - do not need a leading zero */
    {
        strcpy(card, num);
    }
    else                        /* Odd numbers - add a leading zero to evaluate */
    {
        strcpy(card, "0");
        strcat(card, num);
    }

    printf("Length of number is: %d\n", (int)strlen(num));

    for (int i = 0; i < strlen(card); i  )
    {
        work = card[i] - '0';
        if ((i %2) == 0)
        {
            work = (card[i] - '0') * 2;
            if (work > 9)
            {
                work = work - 9;
            }
        }
        sum = sum   work;
        printf("Digit is: %d Value is: %d Sum is %d\n", (card[i]- '0'), work, sum);
    }

    return ((sum % 10) == 0);
}

int main()
{
    char number[20];
    int x = -1;

    printf("Enter a number: ");
    x = scanf("%s", number);

    x = check_valid(number);

    if (x == 0)
        printf("Invalid\n");
    else
        printf("Valid\n");

    return 0;
}

This particular example utilizes the "scanf" function, but one could use the "fgets" function. It usually boils down to preference. But once the value is entered, the individual digits can be evaluated.

Give that a try to see if that meets the spirit of the project.

  •  Tags:  
  • c
  • Related