Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

C program to find the largest element in an array

C program that finds the largest element in an array :

  1. #include
  2. int main() {
  3.   int n, i;
  4.   long long int t1 = 0, t2 = 1, nextTerm;

  5.   printf("Enter the number of terms: ");
  6.   scanf("%d", &n);

  7.   printf("Fibonacci Series: ");

  8.   for (i = 1; i
  9.     printf("%lld, ", t1);
  10.     nextTerm = t1 + t2;
  11.     t1 = t2;
  12.     t2 = nextTerm;
  13.   }

  14.   return 0;
  15. }

  • This program first reads in n, the number of elements in the array. It then reads in n elements from the user and stores them in the array. 
  • Finally, it iterates through the array and compares each element to the first element (which is initially set to the largest). 
  • If it finds a larger element, it updates the value of the first element to be the larger value. At the end, the program prints out the largest element in the array.


  • This program first initializes an array a with 10 elements and assigns the value of the first element to the variable max.
  • It then loops through the array and compares each element with max. 
  • If an element is larger than max, max is updated with the new value. After the loop finishes, max will contain the largest element in the array.


This post first appeared on Java Tutorial - InstanceOfJava, please read the originial post: here

Share the post

C program to find the largest element in an array

×

Subscribe to Java Tutorial - Instanceofjava

Get updates delivered right to your inbox!

Thank you for your subscription

×