In this, we are going to print the largest number among the array elements in C++.
The code given below can be used for TURBO C++ Compiler:-
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int b[5],i,j;
// Taking array elements as input
cout<<"Enter array elements \n";
for(i=0;i<5;i++)
{
cin>>b[i];
}
// comapring array elements
for (i = 1; i < 5; ++i)
{
if (b[0] < b[i])
{
b[0] = b[i];
}
}
cout<<"largest element in the array is "<<b[0];
getch();
}
The code given below can be used for g++/gcc Compiler:-
#include <iostream>
using namespace std;
int main()
{
int b[5],i,j;
// Taking array elements as input
cout<<"Enter array elements \n";
for(i=0;i<5;i++)
{
cin>>b[i];
}
// comapring array elements
for (i = 1; i < 5; ++i)
{
if (b[0] < b[i])
{
b[0] = b[i];
}
}
cout<<"largest element in the array is "<<b[0];
return 0;
}
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP