find 3 largest numbers in an array in c++ code example
Example 1: find the biggest number from 3 numbers c++
#include <iostream>
using namespace std;
int main() {
float n1, n2, n3;
cout << "Enter three numbers: ";
cin >> n1 >> n2 >> n3;
if(n1 >= n2 && n1 >= n3)
cout << "Largest number: " << n1;
if(n2 >= n1 && n2 >= n3)
cout << "Largest number: " << n2;
if(n3 >= n1 && n3 >= n2)
cout << "Largest number: " << n3;
return 0;
}
Example 2: how to get the largest number in a c++ array
#include <iostream>
using namespace std;
int main(){
int n, largest;
int num[50];
cout<<"Enter number of elements you want to enter: ";
cin>>n;
for(int i = 0; i < n; i++) {
cout<<"Enter Element "<<(i+1)<< ": ";
cin>>num[i];
}
largest = num[0];
for(int i = 1;i < n; i++) {
if(largest < num[i])
largest = num[i];
}
cout<<"Largest element in array is: "<<largest;
return 0;
}