Use the "for" loop with "if" to write a program that asks the user to type 15 integers and displays :
1. The smallest of these integers.
2. The largest of these integers
#include <iostream>
int main()
{
  int min, max;
  std::cout << "Input 15 integers: ";
  for(int i = 0; i < 15; ++i)
  {
    int tmp;
    std::cin >> tmp;
    if(!std::cin)
    {
      std::cout << "Error: invalid data\n";
      return 1;
    }
    if(i == 0)
    {
      min = max = tmp;
    }
    else
    {
      if(tmp < min) min = tmp;
      if(tmp > max) max = tmp;
    }
  }
  std::cout << "The smallest of integers is " << min << "\n";
  std::cout << "The largest of integers is " << max << "\n";
  return 0;  Â
}
Comments
Leave a comment