Computer Science, asked by tusharraj77123, 7 hours ago

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 anindyaadhikari13
7

\textsf{\large{\underline{Solution}:}}

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;

}

\textsf{\large{\underline{Logic}:}}

  1. Accept the numbers from the user.
  2. Store the entered number in an array.
  3. 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.
  4. At last, display the numbers.

See attachment for output.

Attachments:
Similar questions