Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8835311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:13:51+00:00 2026-06-14T09:13:51+00:00

I have a code that gets a number from input and then puts it

  • 0

I have a code that gets a number from input and then puts it into array, and then prints said array. But there is very strange side-effect, when I enter 13 digit number, my last digit is wrong. If I enter 15 or 16 digit number everything is fine. I can’t understand what’s happening! Here is the code:
numberDigits is either 12, 14 or 15, based on the number of digits in number.

int myNumber[numberDigits];
    for (int i = 0; i <= numberDigits; i++)
    {
        myNumber[numberDigits - i] = number % 10;
        number = number / 10;         
    } 
    printf("\n");

    for (int i = 0; i <= numberDigits; i++)

    {
        printf("%i",myNumber[i]);
    }

EDIT:
My number is initialized as long long int. Here is a check to verify number of digits:

    if (number / 1000000000000 >= 1 && number / 1000000000000 < 10)
    {
        numberDigits = 12;
    } 
if (number / 100000000000000 >= 1 && number / 100000000000000 < 10)
    {
        numberDigits = 14;
    }
if (number / 1000000000000000 >= 1 && number / 1000000000000000 < 10)
    {
        numberDigits = 15;
    }  

EDIT: Last update, Yes, I have updated my code and it works as expected with your suggestions, but I need to understand why my code doesn’t work. I think that maybe array[12] actually holds 12 elements and not 13, and when I access element array[12] with it, I get some random value from memory, but why this doesn’t happen with array[14] or array[15] with 15 and 16 digit numbers respectively. And why program does not crash as it should when you access array at wrong position. Original question below with old code that doesn’t work.

EDIT: Here is whole listing for those interested.

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

int verify (long long int number);

int main(void)

{
    printf("Please enter card number: \n");
    long long int number = GetLongLong();
    // AMEX uses 15 digits
    if (number > 100000000000000 && ((number / 10000000000000 == 34) || (number / 10000000000000 == 37)))
    {        

        printf("AMEX");

    }

    if (number > 1000000000000 && number < 10000000000000 && (number / 1000000000000 == 4))
    {
        printf("VISA");
    }

    if (number > 1000000000000000 && number < 9999999999999999 && (number / 1000000000000000 == 4))
    {
        printf("VISA");
    }

        if (number > 1000000000000000 && number < 9999999999999999 && (number / 100000000000000 == 51 || number / 100000000000000 == 52 || number / 100000000000000 == 53 || number / 100000000000000 == 54 || number / 100000000000000 == 55))
    {
        printf("MASTERCARD");
    }

    return verify(number); 
}



int verify (long long int number)
{
    int numberDigits;
    if (number / 1000000000000 >= 1 && number / 1000000000000 < 10)
        {
            numberDigits = 12;
        } 
    if (number / 100000000000000 >= 1 && number / 100000000000000 < 10)
        {
            numberDigits = 14;
        }
    if (number / 1000000000000000 >= 1 && number / 1000000000000000 < 10)
        {
            numberDigits = 15;
        }       

    int myNumber[numberDigits];
    for (int i = 0; i <= numberDigits; i++)
    {
        myNumber[numberDigits - i] = number % 10;
        number = number / 10;         
    } 
    printf("\n");

    for (int i = 0; i <= numberDigits; i++)

    {
        printf("%i",myNumber[i]);
    }

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:13:52+00:00Added an answer on June 14, 2026 at 9:13 am

    I think it should be something like this:

    for(int i = 0; i < numberDigits; ++i) printf("%d",myNumber[i]);
    

    instead of:

    for(int i = 0; i <= numberDigits; ++i) printf("%i",myNumber[i]);
    

    And in the first loop:

    myNumber[numberDigits - (i + 1)] = number % 10;
    

    Also, you could use something like this to know the number of digits:

    int digits(long long num) {
      if(!num) return 1;
      return log10(num) + 1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following php code that gets a serialized array from a mysql
If have a piece of code that gets some data from a sql database
I have this code that reads from XML file. It gets five strings (groupId,
In my Java code I have function that gets file from the client in
I have to write program that gets a number n from the user, and
I have some code that gets the current logged in user. userID = request.session.get(_auth_user_id)
I have the following code that gets the MAC address of an interface: static
In PHP I have the following code that gets a file submitted through CGI:
I have a line of code that gets the following error when run through
I have those code snippet that checks the fans table and gets everyone who

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.