Create a program that will ask a user to enter 5 positive numbers.
The program will then display the largest and the smallest value that was entered by the user.
SOLUTION CODE
#include <iostream>
using namespace std;
int main() {
int positive_integers[5];
for(int i = 0; i< 5; i++)
{
cout<<"Enter positive integer "<<(i+1)<<": ";
int my_positive_integer;
cin>>my_positive_integer;
positive_integers[i]=my_positive_integer;
}
int largest = positive_integers[0];
int smallest = positive_integers[0];
for(int i = 0; i<5; i++)
{
if(positive_integers[i]>largest)
{
largest = positive_integers[i];
}
if(positive_integers[i]<smallest)
{
smallest = positive_integers[i];
}
}
cout<<"\nThe largest number entered by the user is "<<largest<<endl;
cout<<"\nThe smallest number entered by the user is "<<smallest<<endl;
return 0;
}
Comments
Leave a comment