Write a program to enter 12 number find largest and smallest number .
C++ for loop program
Don't try to give wrong answer !!
Answers
Answered by
7
The given problem is solved using language C++
#include <iostream>
using namespace std;
int main() {
int n=12, max, min,i,arr[12];
cout<<"Enter 12 numbers...\n\n";
for(i=0;i<12;i++){
cout<<"Enter: ";
cin>>arr[i];
}
max=arr[0];
min=arr[0];
for(i=1;i<12;i++){
if(arr[i]>max)
max=arr[i];
if(arr[i]<min)
min=arr[i];
}
cout<<"\nMax: "<<max<<endl;
cout<<"Min: "<<min;
return 0;
}
- Accept the numbers from the user.
- Store the entered number in an array.
- Assume that largest as well smallest number is at first index. Now loop through array elements. If any number is found greater than max, then initialize max with the number. Do the same for min. If any number is found less than min value then store the number in min.
- At last, display the numbers.
See attachment for output.
Attachments:
Similar questions