Saved Bookmarks
| 1. |
Write a C++ program to find the second largest out of the n numbers in the array. |
|
Answer» #include<iostream.h> #include<iomainp.h> void main() { int a[50],I,n,firstlarge,secondlarge; cout<<"Enter the number of elements"<<end1; cin>>n; cout<<"Enter the elements now :"<<end1; for(i = 0;i<n;I++) cin>>a[1]; if (a[0]>a[1]) { firstlarge = a[0]; secondlarge = a[1]; } else { firstlarge = a[1]; secondlarge = a[0]; } for(i=2;i<n;i++) if(a[i]>firstlarge) { secondlarge = firstlarge; firstlarge = a[i]; } else if (a[i]>secondlarge) secondlarge = a[i]; cout<<"The second largest number is"secondlarge<<endl } |
|