C Program to Find the Greatest Among Ten Numbers

cprogramming-Logo
C Programming Examples
C String Programs
C Loop Programs
C Function Programs
C Array Programs
C Integer Number Programs
C File I/O Programs

This C Program is used to find the greatest among ten numbers.

Program:
#include <stdio.h>
  int main() {
    int a[10];
    int i;
    int greatest;
    printf("Enter ten values:");
    //Store 10 numbers in an array
    for (i = 0; i < 10; i++) {
    scanf("%d", &a[i]);
    }
    //Assume that a[0] is greatest
    greatest = a[0];
    for (i = 0; i < 10; i++) {
if (a[i] > greatest) {
greatest = a[i];
    }
    }
    printf("
    Greatest of ten numbers is %d", greatest);
    return 0;
  }
Program Output:

Enter ten values: 2 53 65 3 88 8 14 5 77 64 Greatest of ten numbers is 88

Explanation:

Entered values are 2, 53, 65, 3, 88, 8, 14, 5, 77, 64
They are stored in an array of size 10. let a[] be an array holding these values.
/* how the greatest among ten numbers is found */
Let us consider a variable 'greatest'. At the beginning of the loop, variable 'greatest' is assigned with the value of the first element in the array greatest=a[0]. Here variable 'greatest' is assigned 2 as a[0]=2.

Below loop is executed until the end of the array 'a[]';.
for(i=0; i<10; i++){
if(a[i]>greatest){
greatest= a[i];
}
}

For each value of 'i', value of a[i] is compared with value of variable 'greatest'. If any value greater than the value of 'greatest' is encountered, it would be replaced by a[i]. After completion of 'for' loop, the value of variable 'greatest' holds the greatest number in the array. In this case, 88 is the greatest of all the numbers.


Scroll Back to Top