taking big integer input all at a time in C++ -
i want take 1000 digit integer input @ time,& want add digits separately.is there input method take such large input?
you need input string. split them, , convert each character integer. add them up, , you're done.
example, number here (randomly generated):
9624526619162264306083309360203157186784123851390498919674886891002552146753945797326679482200717699585297042606470048297021049209667042255911984240697992738371633115195140494325737382583412562136836759072897211537655046343769659111215754043609344618490646811291135643554115350431099553593485744944746093896695837300975718819726339233383800764568364950577294931831936979504756278187812548901366714205562309364234394802723329400976924082450161974562063268243689930750925213262044910428021004262080895556879515597779404780565380480750286553508081070834339176079062215815331059349488936312244526697733596052063044560959189161656978673936732284706841120711543620038686227462170335634371808995466024671420024705248851244350701111587608201303840696489479021196275228499780922745352396928865910631672384263395712487735712098161853665189905194589355110620257494673972892816413534347360049692019184831019218764766067298983043791063184786671132332077197148683743991683245617836086353821268720434176862469084808 and here's c++ program:
int strint(std::string &str) { int i; std::stringstream intstr(str); intstr >> i; return i; } int main () { std::string strdigit, schar; int sum = 0; std::cout << "enter digits: "; std::cin >> strdigit; std::stringstream ss; (int = 0; < strdigit.length(); i++) { ss.clear(); ss << strdigit[i]; ss >> schar; sum += strint(schar); } std::cout << sum; } the sum is: 4479
Comments
Post a Comment