×

# Find sum of digits of very large number.

 0 How to find sum of digits of very large number? ALCATRAZ1 - SUM OF DIGITS asked 16 Dec '16, 23:01 2★rashedcs 497●5●16●54 accept rate: 4% @rashedcs I have added the implementation of both approaches, you can check them to solve the question. (17 Dec '16, 20:52) srd0915★

 2 You can store the number in a string and calculate the sum of digits by reading one character at a time. string s; cin>>s; long long x=0; for(int i=0;i
 0 think of the addition problems that you have done in your primary classes. take input of numbers as strings and you try you'll get it answered 16 Dec '16, 23:13 11●1 accept rate: 0%
 0 C++ The limit of long long int is ~10^19. If you number is bigger than that, take the input as a string, add individual characters. Python : You have no limit on length of integer. Java Use BigInteger answered 16 Dec '16, 23:21 1★s6j4_10 18●2 accept rate: 25% if size is 10^50 the how to solve this.... (17 Dec '16, 08:24) rashedcs2★ @rashedcs I've added implementations using both the approaches mentioned. (17 Dec '16, 13:44) srd0915★ If size is 10^50.....then???? (17 Dec '16, 21:07) rashedcs2★ @rashedcs both Biginteger and string approach will work without any problem. (17 Dec '16, 21:26) srd0915★
 0 @rashedcs You can use either Big integer(for using big integers in c++ you can use boost library) or can take input in string and then perform addition. Implementation using C++ boost library (link) and using char array to store the number (link). answered 16 Dec '16, 23:22 5★srd091 1.6k●1●11 accept rate: 42%
 0 if you have to find the sum of n natural digits then simply the formula n*(n+1)/2 is applicable. answered 17 Dec '16, 12:50 1 accept rate: 0%
 0 Use Big Integer in Java or take the input number as strings and perform addition of individual strings. answered 17 Dec '16, 12:55 1 accept rate: 0%
 0 USE LONG INT OR LONG FLOATING POINTS in C++ answered 17 Dec '16, 15:14 2★amba07 1 accept rate: 0%
 0 C++ users use String to store the digits .. then traversal of string is equivalent to traversal of each digit.. Try this problem to get the hold of it: https://www.codechef.com/problems/DIVNINE answered 17 Dec '16, 21:34 1.2k●10 accept rate: 15%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×1,175

question asked: 16 Dec '16, 23:01

question was seen: 4,149 times

last updated: 17 Dec '16, 22:50