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:
- How to compare dates in java|algorithm with source code
- Java roll dice 10000 times with algorithm and source code
- Write a Java program that displays the number of characters, lines, and words in a text
- Write a Java program that reads a file and displays the file on the screen with a line number before each line
- Write a Java program that reads a file name from the user, then displays information about whether the file exists, readable, writable, type of file, and the length of the file in bytes
- Java program to make frequency count of vowels, consonants, special symbols, digits, words in a given text
- Write a Java program for sorting a given list of names in ascending order
- Write a java program to Checks whether a given string is a palindrome or not
- Write a java program to perform multiplication of two matrices
- write a java program that prints the Fibonacci series for a given number.
- Write a Java program that finds the factorial of a number
- Write a Java program that finds prime numbers between 1 to n
- Write a Java program that prints all real and imaginary solutions to the quadratic equation
- Odd and Even number in java | Algorithm
- Even number in java | algorithm with source code
- Java greater number using a loop
- Java Area of Rectangle
- What is a polygon? | Area of Triangle in java
- To calculate the Area of Circle in the java program
- Java Addition through user input
- Addition program in java with source code
Find the solution to the salesforce Question.
гѓ—гѓ¬гѓ‰гѓ‹гѓі гЃ®иіје…Ґ – гѓ—гѓ¬гѓ‰гѓ‹гѓійЊ 20 mg еј·гЃ• г‚ўг‚ュテインの飲み方と効果