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

  • Home
  • SEARCH
  • 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 1058841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:05:01+00:00 2026-05-16T18:05:01+00:00

This is the second time I have got confusing results from the modulo operator,

  • 0

This is the second time I have got confusing results from the modulo operator, so I am sure I must be missing something about how it works, what am I doing wrong here that is getting me the wrong answers? This function is supposed to take a number and return its digits as a pointer to an array (where the first element says how many digits long it is). What it actually does is returns an array where all the indexes after the first contain the original number.

int *getDigits(int n)
{
        int digits = log10(n)+1;
        int i = 1;
        int *digit = malloc((digits+1) * sizeof(int));
        if (digit == NULL) { printf("error\n"); }
        digit[0] = digits;
        for (i = 1; i < digits+1; i++) {
                int blah = (int) pow(10,i);
                printf("digit[%d] = remainder of %d divided by %d\n",i,n,blah);
                digit[i] = (n%blah);
                printf("%dth digit %d\n",i,n);
        }
        return digit;
}

When I run it each iteration looks something like this:

checking 500996
digit[1] = remainder of 500996 divided by 10
1th digit 500996
digit[2] = remainder of 500996 divided by 100
2th digit 500996
digit[3] = remainder of 500996 divided by 1000
3th digit 500996
digit[4] = remainder of 500996 divided by 10000
4th digit 500996
digit[5] = remainder of 500996 divided by 100000
5th digit 500996
digit[6] = remainder of 500996 divided by 1000000
6th digit 500996
  • 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-05-16T18:05:01+00:00Added an answer on May 16, 2026 at 6:05 pm

    You store the result of the modulo in digit[i], but you do not print that:

    digit[i] = (n%blah);
    printf("%dth digit %d\n",i,n);
    

    n % blah does not modify n or blah, it only returns the result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the second time I've got 4294967294 value in this table. I have
I have got this function in my program. def time(transcriptionFile) : ''' This function
This query takes about 52 seconds to complete. It appears 82% of the time
Ok, I need something like this: datediff(second, date_one, date_two) < 1 dates are stored
I've been following along with this excellent asio tutorial , but have got confused
this is the second time I am posting this on stack overflow forum, the
This is the second time it happens to me and before modifying a 3rd
I think this is my second post regarding Adaptive HTML/CSS layout. I got a
I have a php script that is triggered from my app. This script uses
I have got two tables: tbl_sms tbl_bids The above two table holds something like

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.