C-programming - To find largest number in an array, C program to find largest number
To find largest number in an array
This program finds the largest number in a given array of numbers.#include<stdio.h> #include<conio.h> main() { int array[100], maximum, size, c, location = 1; printf("Enter the number of elements in array\n"); scanf("%d",&size); printf("Enter %d numbers\n", size); for ( c = 0 ; c < size ; c++ ) scanf("%d", &array[c]); maximum = array[0]; for ( c = 1 ; c < size ; c++ ) { if ( array[c] > maximum ) { maximum = array[c]; location = c+1; } } printf("Maximum element is present at location number %d and it's value is %d.\n", location, maximum); return 0; }
An array of elements are got from the user as input by using a for loop. Now the first element of array is assigned as maximum. Then each number in the array is checked to lesser than it. If not lesser, then that number is assigned to the maximum and then again compared with each number in an array. Thus the largest number is found.
output
Enter the number of elements in array 5 Enter 5 numbers 5 4 3 7 1 2 Maximum element is present at location number 4 and its value is 7.
The topic on C-programming - To find largest number in an array is posted by - Maha
Hope you have enjoyed, C-programming - To find largest number in an arrayThanks for your time