In this program, the task is to take input of n no. of elements in , store it in array & then find the largest no. from the elements of array. Here , n is the no. of elements in array & Using for loop , we will get the values in order of a[0] , a[1] , - - - - till a[n] . Afterthat , we assume the largest element is a[0] & then we will check if any no. is larger than the largest element we have , will be stored in variable large using for loop . hence , we will find largest element of array.
#include<stdio.h>
#include<conio.h>
void main()
{ int a[20],i=0,n,large;
clrscr();
printf("\n program to find largest element in array");
printf("\n\n enter no. of elements in array (max 20) : ");
scanf("%d",&n);
printf("\n now enter them : \n");
for(i=0;i<n;i++)
{ printf(" enter a[%d] = ",i);
scanf("%d",&a[i]);
}
printf("\n\n finding largest element now.. \n");
large = a[0];
for(i=0;i<n;i++)
{ if(a[i]>=large)
large = a[i];
}
printf("largest element = %d",large);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{ int a[20],i=0,n,large;
clrscr();
printf("\n program to find largest element in array");
printf("\n\n enter no. of elements in array (max 20) : ");
scanf("%d",&n);
printf("\n now enter them : \n");
for(i=0;i<n;i++)
{ printf(" enter a[%d] = ",i);
scanf("%d",&a[i]);
}
printf("\n\n finding largest element now.. \n");
large = a[0];
for(i=0;i<n;i++)
{ if(a[i]>=large)
large = a[i];
}
printf("largest element = %d",large);
getch();
}

No comments:
Post a Comment
Guys if you think something is wrong or should be edit than please do comment.