- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMissingNumber.java
More file actions
Latest commit
30 lines (22 loc) · 693 Bytes
/
Copy pathMissingNumber.java
File metadata and controls
30 lines (22 loc) · 693 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
packagecom.sarvesh.javabasics;
importjava.util.Scanner;
publicclassMissingNumber {
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
System.out.print("Enter no of elements: ");
intn = sc.nextInt();
int[] arr = newint[n - 1];
System.out.println("Enter elements:");
for (inti=0;i<n-1;i++) {
arr[i] = sc.nextInt();
}
intexpected = n*(n + 1)/2;
intreal = 0;
for (inti=0;i<arr.length;i++) {
real += arr[i];
}
intmissing = expected - real;
System.out.println("Missing number is: " + missing);
sc.close();
}
}