Array
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.
#include <iostream>
using namespace std;
int main()
{
int arr[5];
int minNum,maxNum;
cout<<"Enter 5 positive numbers: ";
for(int i=0;i<5;++i) cin>>arr[i];
minNum=arr[0];
maxNum=arr[0];
for(int i=1;i<5;++i)
{
if (minNum<arr[i]) minNum=arr[i];
if (maxNum>arr[i]) maxNum=arr[i];
}
cout<<"Largest value: "<<maxNum<<endl;
cout<<"Smallest value: "<<minNum<<endl;
return 0;
}
Comments
Thank you so much
Leave a comment