Skip to main content

Find missing number from array range C, C++

  1. Missing number between array's lowest value and highest value elements in C.

         

  1. #include <stdio.h>
  2. int main()
  3. {
  4. int arr[5]={2,5,20,18,15};
  5. int min=arr[0],max=arr[0],i,j,flag=0;
  6. for(i=0;i<5;i++)
  7. {
  8. if(arr[i]>max)
  9. {
  10. max=arr[i]; //find max value
  11. }
  12. if(arr[i]<min)
  13. {
  14. min=arr[i]; //find min value
  15. }
  16. }
  17. for(i=min;i<max;i++)
  18. {
  19. for(j=0;j<5;j++)
  20. {
  21. if(i==arr[j])
  22. {
  23. flag=1;
  24. break;
  25. }
  26. else
  27. {
  28. flag=0;
  29. }
  30. }
  31. if(flag==0){
  32. printf("%d ",i);
  33. }
  34. }
  35. return 0;
  36. }
            
            Output:-

            3 4 6 7 8 9 10 11 12 13 14 16 17 19 

2. Missing number between array's lowest value and highest            value element  in C++.


        
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int arr[5]={2,5,20,18,15};
  6. int min=arr[0],max=arr[0],i,j,flag=0;
  7. for(i=0;i<5;i++)
  8. {
  9. if(arr[i]>max)
  10. {
  11. max=arr[i]; //find max value
  12. }
  13. if(arr[i]<min)
  14. {
  15. min=arr[i]; //find min value
  16. }
  17. }
  18. for(i=min;i<max;i++)
  19. {
  20. for(j=0;j<5;j++)
  21. {
  22. if(i==arr[j])
  23. {
  24. flag=1;
  25. break;
  26. }
  27. else
  28. {
  29. flag=0;
  30. }
  31. }
  32. if(flag==0){
  33. cout<<i<<" ";
  34. }
  35. }
  36. return 0;
  37. }

        Output:-
            
                    3 4 6 7 8 9 10 11 12 13 14 16 17 19
 

Comments

Popular posts from this blog

Android Hello World Application

"Hello World" application. That will display "Hello World" in the middle of the screen in the red color with white background. Now Here We will create a android application that will display "Hello world" in the middle of the screen. Create a Hello Application with empty activity. and Write below mentioned code in your " activity_main.xml" file. <? xml version ="1.0" encoding ="utf-8" ?> < LinearLayout xmlns: android ="http://schemas.android.com/apk/res/android" xmlns: app ="http://schemas.android.com/apk/res-auto" xmlns: tools ="http://schemas.android.com/tools" android :layout_width ="match_parent" android :layout_height ="match_parent" android :gravity ="center" android :background ="#ffffff" tools :context =".MainActivity" > < TextView android :layout_wi...

String Caesar Cipher Program in C and C++

 String Caesar Cipher Program in C              It is one of the simplest encryption technique in which each character in plain text is replaced by a character some fixed number of positions down to it. #include <stdio.h> void main() {     char s[50];     int i, j=0, n;         printf("Enter a String: ");     gets(s);     for(i=0; s[i]!=NULL; i++)     {       s[i] = s[i] + 1;     if (s[i] > 'z')          s[i] = s[i] - 26;     }     printf("%s",s); }          Output:-          Here I also change the case of Vowels in Upper Case.(Optional)   #include <stdio.h> void main() {     char s[50];     int i, j = 0, n;   ...