Post

# Find A Missing Number In Array Without Using Loop In C#

Solution

Check each and every number index. The number is available in the array it will return that number index or it will return -1. After we get index, check the condition is equal to -1 or not. If index equal to -1, that number will print in the Console. Then, check, that number equal to 100. If This not equal to 100, the Increment operator increases value and go to the loop line.
1. using System;
2.
3. namespace PlayingwithCSharp
4. {
5.     public class Print1To100MissingNumberInArrayWithoutLoop
6.     {

7.         public static void MissingNumber()
8.         {
9.             int[] givenArray = new int[99];
10.             int incrementValue = 1, arrayPosion = 0 , one = 1 ;
11.             #region Assign Value to Array
12.             //givenArray[incrementValue] = incrementValue(incrementValue - 1);
13.             givenArray[0] = 1;
14.             givenArray[1] = 46;
15.             givenArray[2] = 34;
16.             givenArray[3] = 23;
17.             givenArray[4] = 26;
18.             #endregion
19.             Array.Sort(givenArray);
20.
21.         Loop:
22.             var isPosition = Array.IndexOf(givenArray, incrementValue);
23.             if(isPosition == -1)
24.             {
25.                 System.Console.Write(incrementValue + " ");
26.                 if (incrementValue != 100)
27.                 {
28.                     arrayPosion++;
29.                     incrementValue++;
30.                     goto Loop;
31.                 }
32.                 else
33.                 {
34.                         System.Console.WriteLine("\nMissing Value 1 to 100 Printed Above");
35.                 }
36.             }
37.             else
38.             {
39.                     if (incrementValue == 100)
40.                     {
41.                         System.Console.WriteLine("\nMissing Value 1 to 100 Printed Above");
42.                     }
43.                     else
44.                     {
45.                         incrementValue++;
46.                         goto Loop;
47.                     }
48.             }