Description
This program is for finding missing odd number in first n odd number. The (n-1) odd number will be any random odd number in the range of first n odd number. The difference between summation of first n odd number and summation of given (n-1) odd number will give the missing odd number.
C/C++
/* C program to Find missing odd number in first n odd number */ //Save it as FindMissingOddNumber.c #include<stdio.h> int main(){ int n; printf("Enter the size of array : "); scanf("%d",&n); //Declaring array for first n odd number int arrOdd[n]; //Declaring array for n-1 odd number out of n odd number to be entered by user int arrGiven[n-1]; int i; //Variable to find sum of entered odd number int sumGiven=0; //Variable to find sum of first n odd number int sumOdd=0; int count=0; printf("Enter %d odd numbers in first %d odd number : ",n-1,n); for(i=0;i<n-1;i++) { scanf("%d",&arrGiven[i]); //Finding sum of entered elements sumGiven += arrGiven[i]; } printf("Entered elements are : "); for(i=0;i<n-1;i++) { printf("%d ",arrGiven[i]); } //Finding first n odd number for(i=0;i<=(n*2);i++) { if(i%2 != 0) { arrOdd[count++] = i; //Finding sum of first n odd numbers sumOdd += i; } } printf("\nThe first %d odd numbers of array are : ",count); for(i=0;i<count;i++) { printf("%d ",arrOdd[i]); } //Printing the missing odd number in first n odd number printf("\nMissing odd number is : %d",(sumOdd-sumGiven)); return 0; }
Input: Enter the size of array : 7 Enter 6 odd numbers in first 7 odd number : 3 9 1 5 13 7 Output: Entered elements are : 3 9 1 5 13 7 The first 7 odd numbers of array are : 1 3 5 7 9 11 13 Missing odd number is : 11
Java
/* Java program to Find missing odd number in first n odd number */ //Save it as FindMissingOddNumber.java import java.io.*; import java.util.Scanner; public class FindMissingOddNumber { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter the size of array : "); int n = scanner.nextInt(); //Declaring array for first n odd number int arrOdd[] = new int[n]; //Declaring array for n-1 odd number out of n odd number to be entered by user int arrGiven[] = new int[n-1]; int i; //Variable to find sum of entered odd number int sumGiven=0; //Variable to find sum of first n odd number int sumOdd=0; int count=0; System.out.println("Enter "+(n-1)+" odd numbers in first "+n+" odd number : "); for(i=0;i<n-1;i++) { arrGiven[i] = scanner.nextInt(); //Finding sum of entered elements sumGiven += arrGiven[i]; } System.out.println("Entered elements are : "); for(i=0;i<n-1;i++) { System.out.print(arrGiven[i]+" "); } //Finding first n odd number for(i=0;i<=(n*2);i++) { if(i%2 != 0) { arrOdd[count++] = i; //Finding sum of first n odd numbers sumOdd += i; } } System.out.println("\nThe first "+count+" odd numbers of array are : "); for(i=0;i<count;i++) { System.out.print(arrOdd[i]+" "); } //Printing the missing odd number in first n odd number System.out.println("\nMissing odd number is : "+(sumOdd-sumGiven)); } }
Input: Enter the size of array : 7 Enter 6 odd numbers in first 7 odd number : 3 9 1 5 13 7 Output: Entered elements are : 3 9 1 5 13 7 The first 7 odd numbers of array are : 1 3 5 7 9 11 13 Missing odd number is : 11
Related Programs
1) Find Smallest element and it’s position of an Array2) Program for finding Second Largest Element Of Array
3) Program to merge two sorted arrays
4) Program to Find the smallest missing number
5) Program to Find K largest elements from array
6) Program to Print How Many Numbers Smaller than Current Number
7) Remove Duplicate Elements From Array
8) Program to Check Array is Perfect or Not
9) Program to Find Mode of a Array
10) Program to Rearrange an array such that arr[i]=i