BeginnersBook.in

Programming For Geeks

  • Home
  • All Tutorials
    • Learn C
    • Learn Python
  • Programs
    • Java Programs
    • C++ Programs
    • C Programs
  • Algorithms
    • Sorting
  • MCQ’s
    • MySQL
    • Operating System
  • WordPress
    • Genesis Pro Themes
    • Blogs
  • Tools
    • Direct Download Link Generator
    • HTML Compiler
  • How to
    • Web Examples
  • Write For Us

C++ Program to Find Largest Element in an Array

May 7, 2018 By Mr. Robot

In this example, you will learn to find the largest element of an array. The program will ask to enter an array of integers and then will find the largest element from the given array.

Example: Find Largest Element in an Array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;
 
int main()
{
    int i, n;
    float arr[100];
 
    cout << "Enter total number of elements: ";
    cin >> n;
 
    // Store number entered by the user
    for(i = 0; i < n; ++i)
    {
       cout << "Enter Number " << i + 1 << " : ";
       cin >> arr[i];
    }
 
    // Loop to store largest number to arr[0]
    for(i = 1;i < n; ++i)
    {
       // Change < to > if you want to find the smallest element
       if(arr[0] < arr[i])
           arr[0] = arr[i];
    }
    //print the largest element
    cout << "Largest element in an array: " << arr[0];
 
    return 0;
}
When you run the program, the output will be following:
1
2
3
4
5
6
7
Enter total number of elements: 5
Enter Number 1 : 13
Enter Number 2 : 15
Enter Number 3 : 35
Enter Number 4 : 87
Enter Number 5 : 64
Largest element in an array: 87

Filed Under: C++ Examples

Related Posts

  • C++ Program to Check Whether Two Strings are Anagram or Not
  • C++ Program to Check Spy Number
  • C++ Program to Solve the 0-1 Knapsack Problem

Recent Posts

  • Download Grand Theft Auto: Vice City Audio File | Highly Compressed
  • Grand Theft Auto: Vice City Game Download For PC | Full Setup
  • SQL SHOW DATABASES Statement
  • Direct view storage Tube (DVST) in Computer Graphics
  • CRT Color Monitor in Computer Graphics

C++ Programs

  • More 300+ Examples
  • “Hello World” Program
  • Add Two Numbers
  • Area of Circle
  • Even or Odd Number
  • Find Factorial Number
  • Simple Calculator Program
  • Swap Two Numbers
  • Check Vowel and Consonant
  • Reverse a Number
  • Fibonacci Series
  • Leap Year
  • Toggle Characters
  • Check Super Digit
  • Check Anagram Strings
  • Spy Numbers
  • Check Automorphic Number
  • Check Neon Number
  • Check Pandigital Number
  • Strong Number
  • Inverse of Matrix

More C++ Examples…

  • Star(*) Pattern Programs
  • Wave Pattern Programs
  • Sorting Algorithms

Tutorials

  • C Tutorial

Copyright © 2019 - BeginnersBook.in - All Right Reserved || Sitemap