Skip to content
Loading
search array n give index
  • You can use IndexOf Property
    0
  • Jignesh Kumar
    Hi,
     
    You can try below code which give you idex of entered element, Put breakpoint and try to understand code which will help you to understand how its working exactly
    1. // DO NOT CHANGE THE FOLLOWING CODE!    
    2. int[] array = new int[10];  
    3. array[0] = 6;  
    4. array[1] = 2;  
    5. array[2] = 8;  
    6. array[3] = 1;  
    7. array[4] = 3;  
    8. array[5] = 0;  
    9. array[6] = 9;  
    10. array[7] = 7;  
    11.   
    12. Console.WriteLine("Search for?");  
    13. int searching = Convert.ToInt32(Console.ReadLine());  
    14.   
    15. // Implement the search functionality here    
    16. var indexofElementinArray = Array.IndexOf(array, searching);  
    17.   
    18. if (indexofElementinArray == -1)  
    19. {  
    20.     Console.WriteLine("the number wasn't found.");  
    21. }  
    22. else {  
    23.     Console.WriteLine("Index of element " + searching + " is :" + indexofElementinArray.ToString());  
    24.   
    25. }  
     
    0
  • Piyush Pansuriya
    Hi, You can use Array.IndexOf.
    1. Console.WriteLine("Search for?");      
    2. int searching = Convert.ToInt32(Console.ReadLine());      
    3. int index = Array.IndexOf(array, searching);    
    4. if(index>=0)  
    5. Console.WriteLine("Index of element=" + index.Tostring());  
    6. else  
    7. Console.WriteLine("number was not found.");  
     "index" will return the index of array..
    +1