This is one of the favorite question of the interviewer and a very easy code. This can be solved by many methods like sorting in ascending order and printing the last element or sorting in descending order and printing the 1st element but we have provided the solution in conventional method. if you need the sorting method solution let us know in the comments.
Program to find Largest Number in an Array
Logic :
- Input size of array
- Input the array
- Set max to Zero
- Run the loop till the length of array
- Check the condition, element of array is greater than max or not
- If greater set max equal to that element and continue the loop
- Once the loop ends print Max
Code :
/***Solution By Coding Demons***/
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++) cin>>a[i];
int max=0;
for(int i=0;i<n;i++){
if(a[i]>max) max=a[i];
}
cout<<max;
}
0 Comments