Description

Welcome to lapmos.com

Write a c program How to find the largest number in an array. 

For Example : 

Given Array : 1 8 3 9 4

Output : 9

So now let's start the code...

#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;
}

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;
}

Ouput :

Enter the size of array : 8
Enter the element : 1 7 2 4 3 9 5 6
The largest number : 9

Comments

Login is mandatory to comment Please login

Recommended Posts

Swap two number | C programming

Write a c program swap two number given by user.

Swap two number without using third variable | C programming language

Write a program Swap two number withtout using third variable in c programming language.

Fibonacci Series | Fibonacci Series in C | @lapmos

Fibonacci numbers ( sequence ) is a set of numbers that start with 1 or 0 followed by a 1 proceeds on the rule that each number.

Palindrome | Palindrome program in c | Palindrome Number

Write a program in c to check that the given number is palindrome or not?

Pascal Triangle | C programming

pascal triangle program in c | Write a c program to print the pascal triangle.

Leap Year | Leap year program in c

Year is divisible by completely 4,100,400 then called that year is leap year.