- 2

C++ Prog 2 Perform Arithmetic Ope. on No. of Size Greater than that of Int Without Using any Data Type of Size Greater than int

C++ Program to Perform Arithmetic Operations on Numbers of Size Greater than that of Int Without Using any Data Type of Size Greater than Int.

3rd Apr 2017, 1:16 PM
RaVVan
RaVVan - avatar
24 Respuestas
+ 18
Consider the numbers as strings. Perform the operations on each character by converting them into corresponding int (subtract ASCII of '0'). For example, While adding, start from the rightmost characters of both numbers and keep adding the equivalent int one by one. The sum should also be a string. Keep track of the extra 1 when the sum is greater than 9. In subtraction, keep track of the borrow when you're subtracting larger number from smaller number. Got this assignment once upon a time and all I remember it freaked me out 😑
3rd Apr 2017, 5:02 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 16
You may get idea from here, I've considered only two numbers of SAME length. https://code.sololearn.com/c4cvASnmle85/?ref=app
3rd Apr 2017, 5:35 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 8
What the heck... i lost after "without using any datatype" line.
3rd Apr 2017, 3:51 PM
Agus Mei
Agus Mei - avatar
+ 6
can you elaborate your question? just to make it clear.
3rd Apr 2017, 4:02 PM
Agus Mei
Agus Mei - avatar
+ 6
Then that will support up to (2^64 - 1)
3rd Apr 2017, 4:46 PM
Cyrus Ornob Corraya
Cyrus Ornob Corraya - avatar
+ 5
Just make it clear and we will answer it.
3rd Apr 2017, 4:07 PM
Cyrus Ornob Corraya
Cyrus Ornob Corraya - avatar
+ 5
like what? this is maybe... int x=3; int y=4; int z=++x *y; cout<<z; // output is 8 or it involves for loop?
3rd Apr 2017, 4:15 PM
Agus Mei
Agus Mei - avatar
+ 5
@Cyrus ahh i see...😐 there is long int datatype in cpp. is that what you mean @Harshit?
3rd Apr 2017, 4:24 PM
Agus Mei
Agus Mei - avatar
+ 5
Nope, it's computer does not support infinity.
3rd Apr 2017, 4:27 PM
Cyrus Ornob Corraya
Cyrus Ornob Corraya - avatar
+ 5
I have tried you cannot even do 2^16 on int
3rd Apr 2017, 4:28 PM
Cyrus Ornob Corraya
Cyrus Ornob Corraya - avatar
+ 5
try long it supports up to (2^32 - 1)
3rd Apr 2017, 4:30 PM
Cyrus Ornob Corraya
Cyrus Ornob Corraya - avatar
3rd Apr 2017, 4:35 PM
Agus Mei
Agus Mei - avatar
+ 5
Wow! there is long long? how much byte does it contains?
3rd Apr 2017, 4:36 PM
Cyrus Ornob Corraya
Cyrus Ornob Corraya - avatar
3rd Apr 2017, 4:44 PM
Agus Mei
Agus Mei - avatar
+ 5
yes
3rd Apr 2017, 4:53 PM
Agus Mei
Agus Mei - avatar
+ 4
can't do that. use long (if there is any in c++) int is 4 bytes max.
3rd Apr 2017, 4:17 PM
Cyrus Ornob Corraya
Cyrus Ornob Corraya - avatar
+ 4
@Agus Mei He wants to do something like this int i = 2e9999999 or 2000000000..... ; he wants unlimited integer.
3rd Apr 2017, 4:21 PM
Cyrus Ornob Corraya
Cyrus Ornob Corraya - avatar
+ 3
You could also create an array of ints and perform arithmetic on that (use each element in the array as a digit that ranges from 0 to 9 and carry over into the next element). I've managed to track integers with 1000 digits using that methodology but adding and multiplying them is much easier than doing things like square root so I depends on what you want to use it for.
3rd Apr 2017, 5:14 PM
Shane
+ 2
practically I want to increase the size of int datatype and perform Arithmetic Operations on it..
3rd Apr 2017, 4:09 PM
RaVVan
RaVVan - avatar
+ 2
@cyrus yup.. but is there anything through which I can increase the range of int datatype explicitly?
3rd Apr 2017, 4:26 PM
RaVVan
RaVVan - avatar