Jay Lopez

Jay Lopez

  • NA
  • 4
  • 5.5k

how to handle an IndexOutOfRangeException in C#

Sep 21 2016 12:14 AM
using System;
namespace Project1
{
class Project1
{
//Main method begins execution of C# app
static void Main(string[] args)
{
int number;//declare number variable
int[] numberDigits = new int[5];//declare array with 5 digits
int i = 4;//counter
//prompt user and reads number
Console.Write("Enter five digit integer:");
number = Convert.ToInt32(Console.ReadLine());
while(number > 0)
{
int digit = number % 10; //extract last digit
numberDigits[i] = digit;
number = number / 10;//divide by 10 to extract next digit
i--;
}
foreach (int digit in numberDigits) Console.Write("{0} ", digit);
Console.WriteLine();
Console.ReadKey();
 
}
}

Answers (6)