Write a C program that finds BOTH the largest and smallest o

Write a C++ program that finds BOTH the largest and smallest out of several integers. Assume that the first integer read specifies the number of values remaining to be entered and that the first number is not one of the integers to compare. Your program should read only one value per input statement. Your program is able to processing arbitrary number of values.

Solution

#include <iostream>
using namespace std;

int main() {
   // your code goes here
   int n,num,largest,smallest;
   cin >> n;
  
   int i=1;
  
   while(i<=n)
   {
   cin >> num;
  
   if(i==1)
   {
   largest = smallest = num;
   }
   else
   {
   if(num>largest)
   largest = num;
  
   if(num<smallest)
   smallest = num;
   }
   i++;
   }
  
   cout << \"Largest number is : \" << largest << endl;
   cout << \"Smallest number is : \" << smallest << endl;
   return 0;
}

Write a C++ program that finds BOTH the largest and smallest out of several integers. Assume that the first integer read specifies the number of values remainin

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site