+ 5
A problem in C ++ with while loop
Requirement: A friend gave you the following challenge and hopes it will get you in trouble:For any number he choose say ,x,you display 10^x (10 to the power of x)on the screen. Input data: On the first line there will be a natural number ,x, with the above meaning Output data: It will displayed on the screen 10^x Restrictions and specifications: 1<x<1500 Exemple Input data output data 5 100000
41 Respostas
+ 11
You did exactly what your friend wanted you to do and fell in the trap.
The real challange in this question is not to calculate exponential value, it is to find some way to store it and print it on console.
Try a large number say 1000 for example in your code to see what am I talking about.
Continue reading for a significant hint :-
The thing here is that there is no primitive c++ datatype which can store number that huge, so you either need a smarter way to store this number or display it without storing.
+ 7
Andrei Macovei did you read my full answer ?
The number you are trying to store will not fit in any PRIMITIVE data type C++ have ( not even long long int )
Also PRINCE TAWIAH you can't use pow() here, it cannot even handle value like 10^50 and you are expecting it to give solution of 10^1500 ?
+ 5
PRINCE TAWIAH that's what I said in my first answer 2 hours ago.
By the way
If you think more carefully, there also exist a much simpler way to solve this problem without even storing that value anywhere.
(I think I should stop giving more hints)š
+ 4
You have to think outside the box. Your friends wants you to display 10^x, not to calculate, not to remember the value, just display. 10^x is the simplest to display because it consists of one 1 and a certain number of 0. Try to use that.
+ 2
PRINCE TAWIAH try inputting 100 in your code and see what is displays.
+ 1
PRINCE TAWIAH I don't understand what's wrong in my code because when I follow exemple show me the result
+ 1
Cyan I use cmath library for rise
+ 1
Analyzed your solution and per your question, since you will be using a loop, you won't need to use the power function.
I will proceed to modifying your code
+ 1
Andrei Macovei
Not sure if this would work but Try to use "if" instead:
if (x>1 && x<1500) {
cout << pow(10, x);
}
return 0;
+ 1
Cyan I tried and it s not good
+ 1
Arsenic so I need a new variable which will hold a huge number is good to be unsigned long long?
+ 1
OK, thanks for the info on the power function
+ 1
PRINCE TAWIAH here is your code in SL's code playground š
https://code.sololearn.com/chUs0ppC41k8/?ref=app
Now try giving 50 as input.
or even something as small as 20
+ 1
Try it on your system then
I can gurentee it will give wrong output for any value greater than even 10.
+ 1
Ok, the problem is also occuring in my ide
+ 1
No it is the limit of the x data type
+ 1
PRINCE TAWIAH are you sure ?
+ 1
Unfortunately, I got no result, nothing can contain such huge integer value. But some people suggest that you use an integer array
+ 1
Maybe this might be useful;
https://amp.reddit.com/r/learnprogramming/comments/4jby4r/c_how_should_i_work_with_10100_numbers/
+ 1
This vide can help you how to handle big numbers
https://youtu.be/SzfViApUBzw