Sep 30, 2011

C Program to find the average of largest two of the given numbers in the array.

Example programs to solve the problems of Arrays in C.
Write a C program to read in four integer numbers into an array and find the average of largest two of the given numbers without sorting the array. The program should output the given four numbers and the average with suitable headings. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                                  Happy Coding
***********************************************************/



#include <stdio.h>
#include <conio.h>
#define MAX 4

void main()
{
 int a[MAX], i, l1,l2,temp;

 clrscr();

 printf("Enter %d integer numbers\n", MAX);
 for (i=0; i < MAX; i++)
 {
  scanf("%d", &a[i]);
 }

 printf("Input interger are\n");
 for (i=0; i < MAX; i++)
 {
  printf("%5d", a[i]);
 }

 printf("\n");

 l1 = a[0];  /*assume first element of array is the first largest*/
 l2 = a[1];  /*assume first element of array is the second largest*/

 if (l1 < l2)
 {
  temp = l1;
  l1 = l2;
  l2 = temp;
 }

 for (i=2;i<4;i++)
 {
  if (a[i] >= l1)
  {
   l2 = l1;
   l1 = a[i];
  }
  else if(a[i] > l2)
  {
   l2= a[i];
  }
 }

 printf("\n%d is the first largest\n", l1);
 printf("%d is the second largest\n", l2);
 printf("\nAverage of %d and %d = %d\n", l1,l2, (l1+l2)/2);

}
Read more Similar C Programs
Array In C

Simple C Programs

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!