+ 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

31st Jan 2021, 10:32 AM
Andrei Macovei
Andrei Macovei - avatar
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.
31st Jan 2021, 10:54 AM
Arsenic
Arsenic - avatar
+ 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 ?
31st Jan 2021, 11:59 AM
Arsenic
Arsenic - avatar
+ 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)šŸ˜…
31st Jan 2021, 1:24 PM
Arsenic
Arsenic - avatar
+ 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.
31st Jan 2021, 7:29 PM
rafal
+ 2
PRINCE TAWIAH try inputting 100 in your code and see what is displays.
31st Jan 2021, 11:04 AM
Arsenic
Arsenic - avatar
+ 1
PRINCE TAWIAH I don't understand what's wrong in my code because when I follow exemple show me the result
31st Jan 2021, 10:42 AM
Andrei Macovei
Andrei Macovei - avatar
+ 1
Cyan I use cmath library for rise
31st Jan 2021, 10:42 AM
Andrei Macovei
Andrei Macovei - avatar
+ 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
31st Jan 2021, 10:48 AM
PRINCE TAWIAH
PRINCE TAWIAH - avatar
+ 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;
31st Jan 2021, 10:53 AM
noteve
noteve - avatar
+ 1
Cyan I tried and it s not good
31st Jan 2021, 10:54 AM
Andrei Macovei
Andrei Macovei - avatar
+ 1
Arsenic so I need a new variable which will hold a huge number is good to be unsigned long long?
31st Jan 2021, 11:14 AM
Andrei Macovei
Andrei Macovei - avatar
+ 1
OK, thanks for the info on the power function
31st Jan 2021, 12:01 PM
PRINCE TAWIAH
PRINCE TAWIAH - avatar
+ 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
31st Jan 2021, 12:58 PM
Arsenic
Arsenic - avatar
+ 1
Try it on your system then I can gurentee it will give wrong output for any value greater than even 10.
31st Jan 2021, 1:02 PM
Arsenic
Arsenic - avatar
+ 1
Ok, the problem is also occuring in my ide
31st Jan 2021, 1:03 PM
PRINCE TAWIAH
PRINCE TAWIAH - avatar
+ 1
No it is the limit of the x data type
31st Jan 2021, 1:04 PM
PRINCE TAWIAH
PRINCE TAWIAH - avatar
+ 1
PRINCE TAWIAH are you sure ?
31st Jan 2021, 1:09 PM
Arsenic
Arsenic - avatar
+ 1
Unfortunately, I got no result, nothing can contain such huge integer value. But some people suggest that you use an integer array
31st Jan 2021, 1:14 PM
PRINCE TAWIAH
PRINCE TAWIAH - avatar
31st Jan 2021, 1:17 PM
PRINCE TAWIAH
PRINCE TAWIAH - avatar
+ 1
This vide can help you how to handle big numbers https://youtu.be/SzfViApUBzw
31st Jan 2021, 1:35 PM
A S Raghuvanshi
A S Raghuvanshi - avatar