Largest Number in a Array

Program Description

In this we have to find the largest element in the array.
For this we have first considered that first element is larger,then we have iterated through the array from 1 to n position to check whether a larger element exists or not.

Program
/*******************************************************************
*
* code.cheraus.com
*
*  A simple program to find largest number in a array.
*
********************************************************************/
#include<stdio.h>
#include<conio.h>

void main()
{
  int max,a[20],n,i;
  clrscr();
  //accepting array size
  printf("\nEnter the array size : ");
  scanf("%d",&n);
  
  //taking input in array
  printf("\nEnter the elements : ");
  for(i=0;i<n;i++)
   scanf("%d",&a[i]);
   
  //printing element
  printf("\nElements are : ");
  for(i=0;i<n;i++)
    printf("%d\t",a[i]);
	
  //finding max element
  max = a[0]; //let us consider 1st element is largest
 
  for(i=1;i<n;i++)
  {
     if(a[i]>max)
	  max=a[i];
   }  
	
  printf("\nLargest Number is : %d",max);
  
  getch();
  	
}

Output
Test Case 1:

Comments
comments powered by Disqus