Find the missing number in an array using C#, Python, Java, and C with code examples.

An array is a collection of similar data types stored in contiguous memory locations. 

In this article, we will discuss how to find the missing number in an array of 1 to 10 using C#, Python, Java, and C programming languages.

C# program to find the missing number in an array of 1 to 10

We will create an array with numbers 1 to 10 where number 5 is missing in the sequence.

Tips:

We can calculate the sum of the numbers within the range [1, N] using the formula N * (N + 1)/2.
Subsequently, we need to find the sum of the elements in the array and subtract this sum from the previously calculated sum of the first N natural numbers.
The result will give you the value of the missing element.

using System;

class Find_Missing_Number
{
    static void Main (string [] args)
    {
        //array to find the missing number between 1 and 10
        // We will take number 1 to 10 where number 5 is missing in the sequence.
        int [] arr = { 1, 2, 3, 4, 6, 7, 8, 9, 10 };

        int missingNumber, Totalsum;
        // According to series rule, calculating sum of total numbers up to 10
        //sum of first n natutal numbers=n* (n+1)/2
       
        Totalsum = (arr.Length + 1) * (arr.Length + 2) / 2;
        // Missing number is calculating.
        foreach (int item in arr)
        {
            Totalsum = Totalsum - item;
        }
        missingNumber = Totalsum;
        Console.WriteLine ($"Missing Number in C#: {missingNumber}");
    }
}

Result:

Missing Number in C#: 5

Code explanation:

  1. The Totalsum variable is calculated using the formula for the sum of the first n natural numbers: (arr.Length + 1) * (arr.Length + 2) / 2.
  2. The code then iterates through each element in the array and subtracting it from Totalsum.
  3. The result is the missing number, which we are printing to the console.

Python program to find the missing number in an integer array of 1 to 10

def find_missing_number_python(numbers):
    expected_sum = 10 * (10 + 1) // 2
    actual_sum = sum(numbers)
    return expected_sum - actual_sum

numbers = [1, 2, 3, 4, 6, 7, 8, 9, 10]
missing_number = find_missing_number_python(numbers)
print(f"Missing Number in Python: {missing_number}")

Result:

Missing Number in Python: 5

Java program to find the missing number in an integer array of 1 to 10

public class MissingNumberJava {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 6, 7, 8, 9,10};
        int missingNumber = findMissingNumberJava(numbers);

        System.out.println("Missing Number in Java: " + missingNumber);
    }

    static int findMissingNumberJava(int[] arr) {
        int expectedSum = 10 * (10 + 1) / 2;
        int actualSum = 0;
        for (int num : arr) {
            actualSum += num;
        }
        return expectedSum - actualSum;
    }
}

Result:

Missing Number in Java: 5

C program to find the missing number in an integer array of 1 to 10

In this C Program, we will create an array with numbers 1 to 10 where number 5 is missing in the sequence.

#include <stdio.h>

int find_missing_number_c(int numbers[], int length) {
    int expected_sum = (length + 1) * (length + 2) / 2;
    int actual_sum = 0;
    
    for (int i = 0; i < length; i++) {
        actual_sum += numbers[i];
    }
    
    return expected_sum - actual_sum;
}

int main() {
    int numbers[] = {1, 2, 3, 4, 6, 7, 8, 9, 10};
    int length = sizeof(numbers) / sizeof(numbers[0]);
    
    int missing_number = find_missing_number_c(numbers, length);
    
    printf("Missing Number in C: %d\n", missing_number);
    
    return 0;
}

Result:

Missing Number in C: 5

Recommended Articles:

Shekh Ali
4 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments