on-internet
← Community

How can I find the maximum element in an array in C?

Stack Overflow ·

I'm a beginner in C programming and I'm learning about arrays.I want to find the largest number in an array and print its index. I wrote this code, but it only prints the largest number, not its position. #include <stdio.h> int main() { int numbers[] = {4, 9, 2, 15, 7}; int max = numbers[0]; for (int i = 1; i < 5; i++) { if (numbers[i] > max) { max = numbers[i]; } } printf("Maximum: %d", max); return 0; } How can I modify my code to print the index of the largest number too?

Comments

Sign in or sign up to comment and upvote.

No comments yet.