How works with large integers ?

Joined
Aug 16, 2022
Messages
1
Reaction score
0
Hello! 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.

C:
#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");
   }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top