#include <stdio.h>
#include <conio.h> // Required for clrscr() and getch()
int main() {
int array[100], maximum, size, c, location = 1;
clrscr(); // Clear the screen (only works in some compilers)
printf("Enter the number of elements in the array:\n");
scanf("%d", &size); // Read the size of the array
printf("Enter %d integers:\n", size);
for (c = 0; c < size; c++)
scanf("%d", &array[c]); // Read the elements into the array
maximum = array[0]; // Initialize maximum to the first element
// Loop to find the maximum value and its location
for (c = 1; c < size; c++) {
if (array[c] > maximum) {
maximum = array[c]; // Update maximum
location = c + 1; // Update location (1-based index)
}
}
// Print the results
printf("Maximum element is present at location %d and its value is %d.\n", location, maximum);
getch(); // Wait for a key press (only works in some compilers)
return 0; // Return success status
}
1. Include Directives
#include <stdio.h>
#include <conio.h>
#include <stdio.h>
: This includes the Standard Input Output library, necessary for functions likeprintf
andscanf
.#include <conio.h>
: This includes the console input/output library, which provides functions likeclrscr()
(to clear the screen) andgetch()
(to wait for a key press). Note thatconio.h
is not part of the standard C library and may not be available in all compilers.
2. Main Function
int main() {
- Defines the
main
function where the program execution starts. (It’s better to useint main()
instead ofvoid main()
for standard compliance.)
3. Variable Declarations
int array[100], maximum, size, c, location = 1;
int array[100]
: An array to store up to 100 integers.maximum
: Variable to store the largest number found.size
: Variable to store the number of elements the user will enter.c
: Loop index variable.location
: Variable to store the position of the maximum element (initialized to 1 for a 1-based index).
4. Clear Screen (Optional)
clrscr(); // Clear the screen
- This function clears the console screen (specific to certain compilers).
5. Input Size of Array
printf("Enter the number of elements in the array:\n");
scanf("%d", &size); // Read the size of the array
- Prompts the user to enter the number of elements they want to input into the array.
6. Input Array Elements
printf("Enter %d integers:\n", size);
for (c = 0; c < size; c++)
scanf("%d", &array[c]); // Read the elements into the array
- Prompts the user to enter the integers for the array and stores them in
array
.
7. Initialize Maximum
maximum = array[0]; // Initialize maximum to the first element
- Initializes
maximum
with the first element of the array.
8. Finding Maximum and Its Location
for (c = 1; c < size; c++)
{
if (array[c] > maximum)
{
maximum = array[c]; // Update maximum
location = c + 1; // Update location (1-based index)
}
}
- This loop starts from the second element (index 1) and checks each element:
- If an element is greater than the current
maximum
, it updatesmaximum
and itslocation
.
- If an element is greater than the current
9. Output the Result
printf("Maximum element is present at location %d and its value is %d.\n", location, maximum);
- Prints the maximum element and its position in the array.
10. Wait for Key Press and Return
getch(); // Wait for a key press
return 0; // Return success status
getch()
waits for the user to press a key before closing the console window (specific to some compilers).return 0;
indicates successful completion of the program.
Output: Enter the number of elements in array 5 Enter 5 integers 2 4 7 9 1 Maximum element is present at location 4 and it's value is 9