0

how to add this without getting an error and getting the exact sum?

int a = 10000000000000; int b = 10000000000000; int c = a + b; Messagebox.Show(Convert.ToString(c)); 
5

2 Answers 2

3

10000000000000 is a higher than int could hold. If you check the max value of int int.MaxValue you get 2147483647. So you should use long there and everything would be ok:

long a = 10000000000000; long b = 10000000000000; long c = a + b; Console.WriteLine(Convert.ToString(c)); 
  • Range of int is [-2147483648,2147483647]

  • Range of long is [-9223372036854775808,9223372036854775807]

Sign up to request clarification or add additional context in comments.

Comments

3

A signed int can only represent numbers between -2,147,483,648 and 2,147,483,647(https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/integral-numeric-types).

You can represent larger range of numbers by using long instead of int.

2 Comments

i want to add numbers larger than long. how should i do it?
@Crys Use decimal else System.Numerics.BigInteger as suggested by @.Vilx: does it fit?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.