You are not logged in. Please login at www.codechef.com to post your questions!

×

Find sum of digits of very large number.

How to find sum of digits of very large number? ALCATRAZ1 - SUM OF DIGITS

asked 16 Dec '16, 23:01

rashedcs's gravatar image

2★rashedcs
49751654
accept rate: 4%

edited 17 Dec '16, 20:35

@rashedcs I have added the implementation of both approaches, you can check them to solve the question.

(17 Dec '16, 20:52) srd0915★

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<s.length();i++)

x+=(int)s[i]-'0';

cout<<x;

in string you can store 10^64-1 digits

link

answered 17 Dec '16, 22:14

purush_ninja's gravatar image

1★purush_ninja
12
accept rate: 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

link

answered 16 Dec '16, 23:13

srihari_v's gravatar image

2★srihari_v
111
accept rate: 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

link

answered 16 Dec '16, 23:21

s6j4_10's gravatar image

1★s6j4_10
182
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★

@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).

link

answered 16 Dec '16, 23:22

srd091's gravatar image

5★srd091
1.6k111
accept rate: 42%

edited 17 Dec '16, 13:41

if you have to find the sum of n natural digits then simply the formula n*(n+1)/2 is applicable.

link

answered 17 Dec '16, 12:50

kartikeyab's gravatar image

0★kartikeyab
1
accept rate: 0%

Use Big Integer in Java or take the input number as strings and perform addition of individual strings.

link

answered 17 Dec '16, 12:55

vinaymatt's gravatar image

0★vinaymatt
1
accept rate: 0%

USE LONG INT OR LONG FLOATING POINTS in C++

link

answered 17 Dec '16, 15:14

amba07's gravatar image

2★amba07
1
accept rate: 0%

edited 17 Dec '16, 20:46

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

link

answered 17 Dec '16, 21:34

pankajkhan's gravatar image

5★pankajkhan
1.2k10
accept rate: 15%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • 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