Finding digits on a number with size 10^100

Hello, I need to write a program that finds the digits of an integer with a maximum size of 10^100. I understand all the algorithms, but I can only use them with regular integers with my current knowledge, I tried using long int, but this represented the numbers like this: 1.e+100
What type should I use? (I hope this is type that I am talking about here, it's different in my native language)
How do I get the number in a format in which I can find the value of each digit?
I am not sure what you're trying to ask. Do you want to count how many digits are in a number? For example, 100 has 3 digits? Or are you trying to do something else?
No, I need to find them. for an example, the digits of 123456789 are 1, 2, 3, 4, 5, 6, 7 ,8, 9
I know how to do this with any regular integer, I just don't know how to do it on a long one, in my case, an integer that would have a size between 0 and 10^20
Last edited on
Please explain your problem better.
Do you want to search the larger for a smaller string of numbers ?
Is your 10^20 number stored in a file ?
If so is the file a text file ?
do you just need to know if a match is found or is there something else you plan to do?
Figure out how to do it for an arbitrary sized string. Store your number as a string. You figure out the rest.
You figure out the rest.

Whoever gave you that assignment is an idiot...
How do you get the number?
Who gives you the number?

should you do it like this?
just print the single numbers?
or do you also have to do some calculation?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

int main()
{
    std::string num;
    
    std::cout << "Enter a number: ";
    std::cin >> num;

    std::cout << "The digits of this number are: \n";
    for(int i = 0; i < num.size(); ++i)
        std::cout << num[i] << ', ';

    return 0;
}
Last edited on
OP's question clearly pegs him as a very confused beginner, so smug answers don't help much. How about something like:

Use long int or long long int. An integer will never print as 1.e+100. Only floating point values will. Make sure you don't convert your value to a float or double.

The trick with this (very common) assignment is to understand:
- basic arithmetic operations on integers (division and remainder)
- loops

To separate out the least-significant (rightmost) digit from a number, use remainder of division by ten:
r = x % 10;

To then remove that digit from the number, use division by ten:
x = x / 10;

Put that stuff in a loop. Can you see when the loop should stop?

Hope this helps.
Duoas wrote:
OP's question clearly pegs him as a very confused beginner, so smug answers don't help much. How about something like:


For the benefit of Duoas:
c4l wrote:
I need to write a program that finds the digits of an integer with a maximum size of 10^100.

...

I know how to do this with any regular integer, I just don't know how to do it on a long one


@OP:
I would, again, direct you towards processing the number as a string. Some of us aren't so smug as to think we can answer a question without reading it.
OP wrote:
I know how to do this with any regular integer, I just don't know how to do it on a long one, in my case, an integer that would have a size between 0 and 10^20

I'm not that smug either. OP is making up value ranges.

If his number really does contain 20 to 100 digits, then yes, processing it as a string the only option he has.

I admit I was responding more to the "prof is an idiot". Sorry.
Topic archived. No new replies allowed.