CSharp - Write program to handle with int type Overflow


You will use long type to store the result of int calculation.


Convert the million into a long type before the calculation starts.

  • Assigning the million to a variable of type long
  • Using an explicit type cast with (long)million


using System;
class Program{/*from   w w  w  .ja v a  2s  .c  o  m*/
    static void Main(string[] args){
        int million = 1000000;

        long millionInLong = million;
        long correctResult = millionInLong * millionInLong;
        Console.WriteLine("Million times million: " + correctResult.ToString("N0"));

        // Alternative calculation of a big valule 
        long r = (long)million * (long)million;
        Console.WriteLine("Million times million: " + r.ToString("N0"));