Problem add and sub two big integer numbers in c++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 3
Reputation: pash11 is an unknown quantity at this point 
Solved Threads: 0
pash11 pash11 is offline Offline
Newbie Poster

Problem add and sub two big integer numbers in c++

 
0
  #1
Apr 6th, 2008
Write a program to add or subtract two big integer numbers. “Big” refers to a number which is more than 30 digits. You can assume that the number may have MAXIMUM 100 digits. Your program should have the data structures to hold such a big number. Also it should have two functions viz Add and Subtract and a main function which demonstrates the usage of Add and Subtract.



e.g of one such operation 12334444446666666666888888812233 + 2222227889999999999 = 12334444446668888894778888812232...

can anyone help me how to solve this problem or how to approach this problem...
its urgent if any sample code or any related url so plz forward it...
Thanks in Advance..

Prashnat
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 7
Reputation: TSharma is an unknown quantity at this point 
Solved Threads: 1
TSharma TSharma is offline Offline
Newbie Poster

make a function out of this code

 
0
  #2
Apr 6th, 2008
make a fuction out of this program and brint to use

  1. #include<iostream>
  2. #include<string>
  3.  
  4. using namespace std;
  5. int main()
  6. {
  7. int l1,k=0, i, l2,j,p;
  8. char s_num1[110], s_num2[110], s_ans[111];
  9. int tens=0, ones, ans;
  10. cin>>s_num1;
  11. cin>>s_num2;
  12. //cout<<(int) s_num1[0];
  13. for(l1=0;s_num1[l1]!='\0';l1++);
  14. for(l2=0;s_num2[l2]!='\0';l2++);
  15. l1--;
  16. l2--;
  17. for(i=l1, j = l2;(i>=0)&&(j>=0);i--,j--){
  18. ans = (int) s_num1[i] + (int) s_num2[j] + tens - 2*48;
  19. ones=ans%10;
  20. tens=ans/10;
  21. s_ans[k] = ones + 48;
  22. k++;
  23. }
  24.  
  25. while(i>=0){
  26. cout<<"str 1 bigger\n";
  27. ans = tens + (int) s_num1[i] -48;
  28. ones=ans%10;
  29. tens=ans/10;
  30. s_ans[k] = ones + 48;
  31. k++;
  32. i--;
  33. }
  34.  
  35. while(j>=0){
  36. cout<<"str2 biigger\n";
  37. ans = tens + (int) s_num2[j] -48;
  38. ones=ans%10;
  39. tens=ans/10;
  40. s_ans[k] = ones + 48;
  41. k++;
  42. j--;
  43. }
  44.  
  45. if(tens!=0)
  46. s_ans[k++] = tens+48;
  47.  
  48.  
  49. cout<<k<<endl;
  50. for(p=k-1;p>=0;p--)
  51. cout<<s_ans[p];
  52.  
  53. return 0;
  54. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC