Menu Bar

Horizontal Scrollable Menu

ads

Search This Blog

Missing Number in java with example

Missing Number in Java
The set of number is number will be given so we have to find the number which is missing inside the the set
example
number={1,2,3,4,5,7,8,9}  
then we have to find the missing number from the given set
Output: 6

source code: 

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);
}
}

No comments:

ads3