fork download
  1. // Online C compiler to run C program online
  2. #include<stdio.h>
  3. int main()
  4. {
  5. int i, max_index, size, max;
  6.  
  7. printf("\n Enter the size of the array: ");
  8. scanf("%d", &size);
  9. int arr[size];
  10.  
  11. printf("Please enter numbers:\n ");
  12.  
  13. for (i = 0; i < size; ++i)
  14. {
  15. scanf("%d", &arr[i]);
  16. }
  17. max = arr[0];//start off assuming that the 1st element is the max
  18. for (i = 0; i < size; i++)//now compare it with the rest of the array, updataing the max all
  19. {
  20. if (arr[i] > max) {
  21. max = arr[i];
  22. max_index = i;
  23. }
  24. }
  25. printf("Largest element = %d at index %d", max, max_index);
  26.  
  27. return 0;
  28. }
Success #stdin #stdout 0s 5360KB
stdin
5 10 20 5 9 1
stdout
 Enter the size of the array: Please enter numbers:
 Largest element = 20 at index 1