Check int overflow - CSharp Language Basics

CSharp examples for Language Basics:int

Description

Check int overflow

Demo Code

using System;// ww  w . j  a  va  2  s  . c o m
class Traveling
{
   public static void Main()
   {
      checked
      {
         int totalTime;
         int totalEnergy;
         int totalRadiation;
         int distance = 2100000;
         int timeFactor = 100000;
         int energyFactor = 20;
         int radiationFactor = 40000;
         totalTime = distance * timeFactor;
         totalEnergy = distance * energyFactor;
         totalRadiation = distance * radiationFactor;
         Console.WriteLine("Total time: " + totalTime);
         Console.WriteLine("Total energy: " + totalEnergy);
         Console.WriteLine("Total radiation: " + totalRadiation);
      }
   }
}

Result


Related Tutorials