For Example:
Given Array: 1 8 3 9 4
Output: 9
// Write a c program to the find the largest number in an array.
#include <stdio.h>
int main()
{
int n;
printf("Enter the size of array : ");
scanf("%d", &n);
int arr[n];
printf("Enter the element : ");
for(int i=0; i<n; i++){
scanf("%d", &arr[i]);
}
int max = arr[0];
for(int i=0; i<n;i++){
if(arr[i]>max){
max = arr[i];
}
}
printf("The largest number : %d", max);
return 0;
}
// Write a c program to find the largest number in an array using function.
#include <stdio.h>
int largest_num(int arr[], int size);
int main()
{
int n;
printf("Enter the size of array : ");
scanf("%d", &n);
int arr[n];
printf("Enter the element : ");
for(int i=0; i<n; i++){
scanf("%d", &arr[i]);
}
int max = largest_num(arr, n);
printf("The largest number : %d", max);
return 0;
}
int largest_num(int arr[], int size){
int max = arr[0];
for(int i=0; i<size;i++){
if(arr[i]>max){
max = arr[i];
}
}
return max;
}
Comments
Oops!