Missing Number in java with example

How do you find the missing number in an array?

In this program, we will learn to find the missing number from the given list of numbers.
  • The sum of n sequential numbers will be [n*(n+1)]/2. Using this get the sum of the numbers the n numbers.
  • Add all the elements in the array.
  • Subtract the total of the numbers in the array from the total of the n numbers.
example
number={1,2,3,4,5,7,8,9} Now we have to find the missing number from the given set to find that we will use the formula:  [n*(n+1)]/2

Source Code:

First, enter how many numbers are available in the list then enter the lists of numbers in the sequence. If any number is missing on that sequence then it will print that number.

import java.io.*;
import java.util.*;
class Missno
{
public static void main(String arg[])
{
Scanner obj=new Scanner(System.in);
System.out.println("enter n value");
int n=obj.nextInt();
int sum=0;
int c,i,a[]=new int[n];;
System.out.println("enter values");
for(i=0;i<n-1;i++)
{
a[i]=obj.nextInt();
sum=sum+a[i];
}
c=(n*(n+1)/2)-sum;
System.out.println("missing no is: "+c);
}
}

Output:

Output:
PS C:\Users\Pramod> & 'c:\Users\Pramod\.vscode\extensions\vscjava.vscode-java-debug-0.31.0\scripts\launcher.bat' 'C:\Program Files\Java\jdk-15.0.2\bin\java.exe' '--enable-preview' '-XX:+ShowCodeDetailsInExceptionMessages' '-Dfile.encoding=UTF-8' '-cp' 'C:\Users\Pramod\AppData\Local\Temp\vscodesws_624b1\jdt_ws\jdt.ls-java-project\bin' 'Missno'
enter n value
5
enter values
1
2
4
5
missing no is: 3
PS C:\Users\Pramod>

Recommended Post:

Find the solution to the salesforce Question.

Spread the love

6 thoughts on “Missing Number in java with example”

Leave a Reply

%d bloggers like this: