In: Computer Science
• Write a C++ program to find the largest umber, smallest number and sum of all the element of a given array of 20 integers
• Note
− Declare array to 20 numbers
− Input values for 20 array elements
− Find largest number
− Find smallest number
− Find sum of all numbers
• Display the results
C++ program:
#include <iostream>
using namespace std;
int main()
{
int i, N,s;
float a[20];
N=20;
for(i = 0; i < N; ++i)
{
cout << "Enter Number " << i + 1 << " : ";
cin >> a[i];
}
//Largest number
for(i = 1;i < N; ++i)
{
if(a[0] < a[i])
a[0] = a[i];
}
cout << "Largest Number = " << a[0];
cout<<"\n";
//smallest number
for(i = 1;i < N; ++i)
{
if(a[0] > a[i])
a[0] = a[i];
}
cout << "Smallest Number = " << a[0];
cout<<"\n";
//sum of all the elements
for(i = 1; i < N; ++i)
{
s += a[i];
}
cout << "Sum of all numbers= " << s;
return 0;
}
Output: