ahmed0saber

Find the smallest 2 values in C++

Nov 5th, 2020
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int x , y , i , n , a[10];
  6.     cout<<"Enter the number of values : ";
  7.     cin>>n;
  8.     for(i=0;i<n;i++)
  9.     {
  10.         cout<<"Enter the value number "<<i+1<<" : ";
  11.         cin>>a[i];
  12.     }
  13.     x=a[0];
  14.     for(i=0;i<n;i++)
  15.     {
  16.         if(a[i]<x)
  17.         {
  18.             x=a[i];
  19.         }
  20.     }
  21.     if(a[0]!=x)
  22.     {
  23.         y=a[0];
  24.     }
  25.     else
  26.     {
  27.         y=a[1];
  28.     }
  29.     for(i=0;i<n;i++)
  30.     {
  31.         if(a[i]==x)
  32.         {
  33.             i=i+1;
  34.         }
  35.         if(a[i]<y)
  36.         {
  37.             y=a[i];
  38.         }
  39.     }
  40.     cout<<endl<<"The smallest two values are "<<x<<" and "<<y;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment