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.
Table of Contents
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:
- The
Totalsum
variable is calculated using the formula for the sum of the first n natural numbers:(arr.Length + 1) * (arr.Length + 2) / 2
. - The code then iterates through each element in the array and subtracting it from
Totalsum
. - 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:
- Program to copy all elements of an array into another array
- Program To Find Largest Number In An Array
- Remove Duplicate Elements from an Array
- Convert a 2D array into a 1D array
- C# program to find the maximum and minimum number in an array
- Different Ways to Calculate Factorial in C# (with Full Code Examples)
- How to remove duplicate characters from a String in C#
- C# program to count the occurrences of each character in a String
- C# program to count vowels and consonants in a string
- C# Program to Check if a Given Number is Even or Odd
- Leap Year Program in C, C++, C#, JAVA, PYTHON, and PHP
- Program to print prime numbers from 1 to N
- C# Programs asked in Interviews
- Difference Between Array And ArrayList In C#: Choosing the Right Collection - May 28, 2024
- C# Program to Capitalize the First Character of Each Word in a String - February 21, 2024
- C# Program to Find the Longest Word in a String - February 19, 2024