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 8816089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:32:09+00:00 2026-06-14T04:32:09+00:00

So I have a four digit number that is player-input in a simple puzzle

  • 0

So I have a four digit number that is player-input in a simple puzzle I am making, I want to be able to check each digit, say I want to check the second digit to the right, if the number happens to be 4601 then of course it will be 6 but is their a faster way other than testing every single four digit number?
I found a few results with search but they didn’t help, they just confused me more, please phrase any answers so anyone can understand them.
Also i am using c++.

  • 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-14T04:32:10+00:00Added an answer on June 14, 2026 at 4:32 am

    To retrieve the second most significant (base ten) digit from an arbitrary integer i you could do:

    while (i >= 100)
        i /= 10;
    return i % 10;
    

    Of course, this assumes the number greater than or equal to 10 to begin with. If you need to preserve the number, then you will (obviously) want to be operating on a copy.

    EDIT:

    One could define a function for extracting an arbitrary digit using either arithmetic or string operations.

    Arithmetic solution:

    int extractDigit(size_t digit, int n) {
        int mask = 1;
        while ( digit --> 0 )
            mask *= 10;
        if (n < mask / 10) { // insufficient digits
            return -1; // or some other appropriate error handling.
        while ( n >= mask )
            n /= mask;
        return n % 10;
    }
    

    String solution:

    #include <sstream>
    #include <string>
    using std::string;
    using std::stringstream;
    
    int extractDigit(size_t digit, int n) {
        string result = static_cast<stringstream&>(stringstream() << n).str();
        if (result.size() < digit) {
            return -1;
        }
        return result[digit-1] - '0';
    }
    

    Both of these solutions implicitly assume that n will be non-negative. You could enforce this pre-condition by using an unsigned data type if you need to. Also, both of these functions are defining the digit positions such that the most significant is in position 1, and the positions increase to the right.

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

Sidebar

Related Questions

I have very simple piece of code. The goal is when i input four-digit
I would like to create a route that allows for any four digit number
I want to match and replace a number of four digit numbers in a
i have four radio buttons , and i want to set a text for
i have four tables user-question contains two columns: questionID, userID, the questions that the
i have four imageviews in a linear layout in such a manner that only
I have a regex that correctly captures a slash followed by a number in
First, I want to say that I'm out on deep water here, since I'm
I want to only show the last four digits of a Social security number
I have a file called !input.txt containing multiple lines, each line is a random

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.