C program that finds the largest element in an array :
- #include <stdio.h>
- int main() {
- int n, i;
- long long int t1 = 0, t2 = 1, nextTerm;
- printf("Enter the number of terms: ");
- scanf("%d", &n);
- printf("Fibonacci Series: ");
- for (i = 1; i <= n; ++i) {
- printf("%lld, ", t1);
- nextTerm = t1 + t2;
- t1 = t2;
- t2 = nextTerm;
- }
- return 0;
- }
- 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.
No comments