Q:

C Program To Find Largest Number In An Array

belongs to collection: Array Programs in C

0

you have to make this program in the following way:

  • C Program To Find Largest Number In An Array Using Recursion
  • C Program To Find Largest Number In An Array Using Pointer
  • C Program To Find the 2nd Largest Number In An Array

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C Program To Find Largest Number In An Array Using Recursion

Program -:

//C Program To Find Largest Number In An Array Using Recursion

#include<stdio.h>
int Largest(int a[],int n);

void main()
{
    int i,j,n,a[20];
    printf("Enter a Number :");
    scanf("%d",&n);
    printf("Enter Values : ");

    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }

    printf("\%d is Largest Number",Largest(a,n));
}
int Largest(int a[],int n)
{
    int max;

    if(n==1)
        return a[0];

    else {
        max=Largest(a,n-1);

        if(max>a[n-1])
            return max;
        else

            return a[n-1];
    }

}

Output -:

Enter a Number : 5
Enter Values : 10 
20
301
391
213
391 is Largest Number

C Program To Find Largest Number In An Array Using Pointer

Program -:

//C Program To Find Largest Number In An Array Using Pointer

#include<stdio.h>
void  main()
{
    int i,*ptr, n,a[100],larg;

    printf("Enter How many number you want?:\n") ;
    scanf("%d",&n) ;

    printf("Enter %d numbers\n",n) ;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]) ;
    }
    ptr = &a[0];
    larg= *ptr;
    for(i=0;i<n;i++,*ptr++)
    {
        if(*ptr>larg)
        {
            larg=*ptr;
        }
    }
    printf("%d is Largest Number",larg);
}

Output -:

Enter How Many Number You Want?
5
Enter 5 Numbers
10
3281
28321
33212
2434

33212 is Largest Number

C Program To Find 2nd Largest Number In An Array

Program -:

//C Program To Find the 2nd Largest Number In An Array

#include<stdio.h>
void  main()
{
    int i,n,array[100],Largest, secLargest;

    printf("Enter How many number you want?:\n") ;
    scanf("%d",&n) ;

    printf("Enter %d Values \n",n) ;
    for(i=0;i<n;i++)
    {
        scanf("%d",&array[i]) ;
    }
   if (array[0] > array[1]) {
        Largest = array[0];
        secLargest = array[1];
    }
    else {
      Largest = array[1];
      secLargest = array[0];
    }
    for (i = 2; i < n; i++) {
        if (array[i] > Largest) {
        secLargest = Largest;
        Largest = array[i];
        }
        else if (array[i] > secLargest) {
            secLargest = array[i];
        }
    }
    printf(" \nSecond Largest Element is %d", secLargest);
}

Output -:

Enter How Many Number You Want?
5
Enter 5 Numbers
10
321
2821
312
2434

Second Largest Element is 2434

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C Program To Find Largest And Smallest Number In A... >>
<< C Program To Find Smallest Number In An Array (3 D...